Left join fetch with condition fails

◇◆丶佛笑我妖孽 提交于 2019-12-25 04:06:10

问题


I have the following HQL query:

return entityManager().createQuery(
    "SELECT page FROM ProjectPage page"
  + " left join fetch page.categorySet as category              "
  + " where page.id = :id " 
  + " and  category.parentCategory is null "
  + " and  (category.status != :status_val) " 
  ,ProjectPage.class).setParameter("id", id)
  .setParameter("status_val", 1).getSingleResult();

the problem is that the conditions in the where clause fails, for example, the query returns category objects whose status is 1 and category objects whose parentCategory is not null although i specified this constrains as stated above!!


回答1:


If you expect this query to return ProjectPage object with categorySet filtered out based on where conditions, then your expectations are wrong. If ProjectPage instance with given id contains any category that pass the where clause conditions, it will be returned as a whole object. This is by design, and needed because of underlying mechanisms, caching, etc. If you need category objects that fulfill some conditions, you'll have to write a separate query for that.



来源:https://stackoverflow.com/questions/27939825/left-join-fetch-with-condition-fails

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