membersound
membersound

Reputation: 86687

How to select a specific entry in a List from DB?

How can I query for a specific project if I have 2 parameters username and projectname? Especially, what's wrong with the following query?

class Person {
   private String name;
   private List<Project> projects;
}

class Project {
   private String projectname;
}

Query:

SELECT Project FROM Person p WHERE p.name := username AND p.projects.projectname =: projectname

Result:

illegal attempt to dereference collection [person0_.name.projects] with element property reference [projectname]

Upvotes: 0

Views: 52

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 121998

since you are doing operations on collection of your pojo ,You have to eagerly load the collection before doing on operations on it.

Try(Didn't test it)

SELECT Project FROM Person p left join fetch p.projects as projs WHERE
                        p.name := username AND  projs.projectname =: projectname

Please refer association joins in HQL

Upvotes: 1

Related Questions