HQL:HQL查询中的多个内部联接

时间:2013-06-28 07:29:09

标签: hibernate join orm hql

我的项目中有很多实体,我想通过HQL查询从多个实体中检索数据。在HQL查询中,我尝试使用JOINS从实体中检索数据,但是在执行代码时会生成异常。以下是例外:

org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [SELECT s.id, s.name, s.url, s.uuid, s.createdBy , t.id , u.id , u.name FROM com.insonix.qrata.models.Site s INNER JOIN s.topics t INNER JOIN User u  WHERE lower(s.name) LIKE lower(:name)  AND s.createdBy IN (:created_by) AND u.id = s.createdBy]

以下是查询:

SELECT s.id, s.name, s.url, s.uuid, s.createdBy , t.id , u.id , u.userinfo.firstname , u.userinfo.lastname FROM Site s INNER JOIN s.topics t INNER JOIN User u WHERE lower(s.name) LIKE lower(:name)  AND s.createdBy IN (:created_by) AND u.id = s.createdBy

以下是我的实体:

public class CommonEntity {
-------------------
 private Long createdBy;
 private String uuid;
------------------------
}

public class Site extends CommonEntity{
 private long id;
 private String url;
 private String name;
 private Category category;
 private List<Topic> topics = new ArrayList<>(0);
  ---------------------------
}

public class Topic extends CommonEntity{
 private int id;
 private List<Site> sites = new ArrayList<Site>(0);
----------------------------------- 
}

public class User extends CommonEntity
 private long id;
 private UserInfo userinfo;
--------------------------------
}

public class UserInfo extends CommonEntity{
 private String firstname;
 private String lastname;
}

1 个答案:

答案 0 :(得分:2)

路径预期错误显示hibernate正在尝试查找关联或映射b / w用户和站点。对于hibernate加入实体,您必须具有已定义的关联。在hibernate中没有ON子句,因此在上面的查询中你将如何指定,在哪里加入'INNER JOIN User u ON ???',(https://stackoverflow.com/a/12669051/830945

您可以像这样获得所需的结果

SELECT s.id, s.name, s.url, s.uuid, s.createdBy, t.id , u.id , u.userinfo.firstname , u.userinfo.lastname FROM Site s, User u INNER JOIN s.topics t WHERE lower(s.name) LIKE lower(:name)  AND s.createdBy IN (:created_by) AND u.id = s.createdBy
相关问题