Reputation: 291
In Spring Intreface I have following code:
@Query("select gl from GroupEntity gl where ?1 in gl.contacts ")
List<GroupEntity> findGroupsByContact(ContactEntity contactEntity);
and want to find all Group with contains a Contact, but IntelliJ idea shows error after where condition ( ?1 in gl.contacts) and shows this error:
'(' or <input parameter> expected, got 'gl'
what is the solution?
related parts of Entities are:
@Entity
@Table(name = "contactGroup")
public class GroupEntity {
...
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<ContactEntity> contacts;
...
}
but in ContactEntity did not use @ManyToMany for GroupEntity(Is it the root of the problem?)
@Entity
@Table(name = "contact")
public class ContactEntity {
...
}
Upvotes: 1
Views: 1947
Reputation: 736
If the relationships are established properly then you can query Groups from Contact repository.
Group.java
@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "group", orphanRemoval = true)
List<Contact> contacts = new ArrayList<Contact>();
Contact.java
@ManyToOne
Group group;
Upvotes: 2
Reputation: 2246
You can't use the in
JPQL operator when the entity attribute is the right operant.
You should use member of
instead.
"Member of" should be used when you provide a value to the query in order to check if it belongs to some collection in your entity.
"In" should be used when you provide a list of values to the query in order to check if a property in your entity belong to this list.
Examples:
@Entity
public class EntityA {
@Id
private String id;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<EntityB> entities;
}
JPQL:
SELECT a FROM EntityA a WHERE a.id IN :ids
SELECT a FROM EntityA a WHERE :b MEMBER OF a.entities
Upvotes: 3