Inner join using HQL

那年仲夏 提交于 2019-12-10 02:42:04

问题


I am trying to inner join two tables on one column. From DB side, there's no mapping as it's something I don't want to discuss.

I want to execute HQL query using INNER JOIN and retrieve ROLE objects/results.

Here's my hql so far

session.createQuery("from ROLE as role INNER JOIN INVOLVEMENT as involvement ON role.id = involvement.roleid WHERE involvement.id = X").list();

I see ON is not available on HQL. how do i explicitly tell Hibernate to JOIN on this column only.

I tried below one too

select roleSpec from ROLE as role, INVOLVEMENT as involvement WHERE role.ID = involvement.role_id and involvement.id =27251352

But I am getting ROLE not mapped in exception.


回答1:


Please check that your ROLE is indeed a mapped entity. In addition, you don't need to perform "ON" - hibernate knows what is the join column (I know how to define this at JPA ) - so no need to provide it at the statement. It should be -

session.createQuery("from Role as role INNER JOIN Involvement as involvement WHERE involvement.id = X").list();

I assume you have Role class mapped to ROLE table, and Involvement class mapped to Involement table.
Maybe you used table names by mistake, and this is why you get the "not mapped" error.
Last time I wrote HQL (and not JPA-QL) I used the following link as reference, it provides all the info needed.



来源:https://stackoverflow.com/questions/13412112/inner-join-using-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!