如何从DB中选择列表中的特定条目?

时间:2013-06-28 11:27:30

标签: java sql hibernate jpa hsqldb

如果我有2个参数usernameprojectname,如何查询特定项目?特别是,以下查询出了什么问题?

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

class Project {
   private String projectname;
}

查询:

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

结果:

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

1 个答案:

答案 0 :(得分:1)

因为您正在收集pojo的操作,所以在对其进行操作之前,您必须急切地加载该集合。

尝试(没有测试)

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

refer association joins in HQL