如何使用 Hibernate 的 session.createSQLQuery() 来使用 JOIN

发布于 2024-10-11 18:48:47 字数 1142 浏览 5 评论 0原文

我有两个实体(表) - 员工和员工项目。一个员工可以有多个项目。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-10-18 18:48:47

加载 EmployeeDTO 时,您不需要使用本机 SQL 查询来预取 ProjectDTO。您可以以更优雅的方式重写原始查询,如下所示:

select e from EmployeeDTO e join e.projects p where p.id = ?1

然后您可以添加 join fetch 子句以预取项目:

select distinct e from EmployeeDTO e join e.projects p join fetch e.projects where p.id = ?1

另请参阅:

You don't need to use a native SQL query to prefetch ProjectDTOs when loading EmployeeDTO. Your original query can be rewritten in more elegant way as follows:

select e from EmployeeDTO e join e.projects p where p.id = ?1

Then you can add a join fetch clause in order to prefetch the projects:

select distinct e from EmployeeDTO e join e.projects p join fetch e.projects where p.id = ?1

See also:

一个人的旅程 2024-10-18 18:48:47

您的 SQL 以 SELECT E from EMPLOYEE E ... 开头,您的意思是 SELECT E.*, P.* from EMPLOYEE E ... 吗?

Your SQL starts with SELECT E from EMPLOYEE E ..., do you mean SELECT E.*, P.* from EMPLOYEE E ...?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文