1

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

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;
}
4

1 回答 1

2

路径预期错误表明休眠正在尝试查找关联或映射黑白用户和站点。要让 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
于 2013-07-01T09:18:53.897 回答