HQL, left join on the same table

こ雲淡風輕ζ 提交于 2019-12-12 10:54:24

问题


I search a way to do a left join with the same table with hql.

It's my query

  FROM Tvshow e
  LEFT JOIN Tvshow e1 ON e1.num = e.num
 WHERE e1.code = '024'
   AND e.code is not null
   AND e.code != '024'

Hibernate don't seem to like on operator.


回答1:


Left joins in HQL are only possible if you have an association between two entities. Since your query imposes the joined entity to be non null, an inner join would do the same thing. An inner join, with the join syntax, is also possible only if you have an association between two entities. But you can do it by simply adding an equality test in the where clause:

select e from Tvshow e, Tvshow e1
where e.num = e1.num
and e1.code = '024'
and e.code is not null
and e.code != '024'



回答2:


I don't use hibernate but, judging by this example:

from Cat as cat
inner join cat.mate as mate
left outer join cat.kittens as kitten

From this page: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins-forms

It looks like you just don't do an ON? Let me know if I'm mistaken and I'll take this down.

FROM Tvshow e
  LEFT JOIN Tvshow e1 
 WHERE e1.code = '024'
   AND e.code is not null
   AND e.code != '024'


来源:https://stackoverflow.com/questions/11741439/hql-left-join-on-the-same-table

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