How do I check for the existence of an object in an arraylist of instances of the same class in a HQL query?
For the ease of understanding I'll paste the entire method in which I'm trying to accomplish this:
public List<Relation> findRelations(List<Organisation> subjectOrganisations) {
return emFactory.get()
.createQuery("FROM Relation rel WHERE rel.subject.id in (SELECT r.id FROM RelationEntityOrganisation r where r.organisation in :subjectOrganisations)", Relation.class)
.setParameter("subjectOrganisations", subjectOrganisations).getResultList();
}
I think the problem is caused by me trying to check the existence of an organisation instance in an ArrayList in an illegal way namely the "where r.organisation in :subjectOrganisations
" part of the query.
This is the Error stacktrace that I get when I try to run the query:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ) near line 1, column 192 [FROM com.company.you.server.domain.entity.Relation rel WHERE rel.subject.id in (SELECT r.id FROM com.company.you.server.domain.entity.RelationEntityOrganisation r where r.organisation in )] at (...) Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ) near line 1, column 192 [FROM com.Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ) near line 1, column 192 [FROM com.company.you.server.domain.entity.Relation rel WHERE rel.subject.id in (SELECT r.id FROM com.company.you.server.domain.entity.RelationEntityOrganisation r where r.organisation in )] (...) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1254) at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573) at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449) ... 60 more
I have already found this article but it doesn't answer my question because I want to inject an array of the type ArrayList into the HQL.
I hope someone can help me out.