如何使用 Hibernate 的 session.createSQLQuery() 来使用 JOIN
我有两个实体(表) - 员工和员工项目。一个员工可以有多个项目。 Project 表的 CREATOR_ID 字段引用 Employee 表的 ID 字段。员工实体不维护项目的任何引用 - 但项目实体有员工的引用。
使用 EntityManager 以下查询工作正常 -
entityManager.createQuery(
"select e from EmployeeDTO e, ProjectDTO p"
+ " where p.id = ?1 and p.creator.id=e.id");
但由于我有 LAZY 关联关系,所以出现错误:
无法初始化代理 - 否 会话
如果我尝试从员工实体访问项目信息, 。这是预期的,所以我使用 Hibernate 的 Session 来创建查询,如下所示。
Session session = HibernateUtil.getSessionFactory().openSession();
org.hibernate.Query q = session.createSQLQuery(
"SELECT E FROM EMPLOYEE_TAB E, PROJECT_TAB P WHERE P.ID = "
+ projectId + " AND P.CREATOR_ID = E.ID")
.addEntity("EmployeeDTO ", EmployeeDTO.class)
.addEntity("ProjectDTO", ProjectDTO.class);
但我收到如下错误:“列 'E' 要么不在 FROM 列表中的任何表中,要么出现 在连接规范内并且在连接规范的范围之外...”
任何人都可以建议对于这种情况正确的 JOIN 语法是什么?如果我使用 ("SELECT * FROM EMPLOYEE_TAB E, ..... ...")
- 它给出了其他错误:
java.lang.ClassCastException: [Ljava.lang.Object;无法投射到 com.im.server.dto.EmployeeDTO
。
提前致谢。
I have two Entity (tables) - Employee & Project. An Employee can have multiple Projects.
Project table's CREATOR_ID field refers to Employee table's ID field. Employee entity does not maintain any reference for Project - but Project entity has a reference for Employee.
Using EntityManager following query works fine -
entityManager.createQuery(
"select e from EmployeeDTO e, ProjectDTO p"
+ " where p.id = ?1 and p.creator.id=e.id");
But since I have the LAZY association relationship, I get error:
Could not initialize proxy - no
Session
if I try to access Project info from Employee entity. This is expected and so I am using Hibernate's Session to create query as shown below.
Session session = HibernateUtil.getSessionFactory().openSession();
org.hibernate.Query q = session.createSQLQuery(
"SELECT E FROM EMPLOYEE_TAB E, PROJECT_TAB P WHERE P.ID = "
+ projectId + " AND P.CREATOR_ID = E.ID")
.addEntity("EmployeeDTO ", EmployeeDTO.class)
.addEntity("ProjectDTO", ProjectDTO.class);
But I get error like: "Column 'E' is either not in any table in the FROM list or appears
within a join specification and is outside the scope of the join specification..."
Can anyone suggest what will be the right JOIN syntax for such case? If I use ("SELECT * FROM EMPLOYEE_TAB E, ........")
- it gives other error:
java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to
com.im.server.dto.EmployeeDTO
.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
加载
EmployeeDTO
时,您不需要使用本机 SQL 查询来预取ProjectDTO
。您可以以更优雅的方式重写原始查询,如下所示:然后您可以添加
join fetch
子句以预取项目:另请参阅:
You don't need to use a native SQL query to prefetch
ProjectDTO
s when loadingEmployeeDTO
. Your original query can be rewritten in more elegant way as follows:Then you can add a
join fetch
clause in order to prefetch the projects:See also:
您的 SQL 以
SELECT E from EMPLOYEE E ...
开头,您的意思是SELECT E.*, P.* from EMPLOYEE E ...
吗?Your SQL starts with
SELECT E from EMPLOYEE E ...
, do you meanSELECT E.*, P.* from EMPLOYEE E ...
?