条件失败的左连接提取 [英] Left join fetch with condition fails

查看:106
本文介绍了条件失败的左连接提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 HQL 查询:

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();

问题是where子句中的条件失败,例如,查询返回类别对象状态为1,并且parentCategory不为null的类别对象,尽管我指定此约束如上所述!!

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!!

推荐答案

查询返回 ProjectPage 对象与 categorySet 过滤掉基于其中条件,那么你的期望是错误的。如果 ProjectPage 具有给定ID的实例包含通过 where 子句条件的任何将作为整个对象返回 。这是通过设计,因为底层机制,缓存等需要。如果你需要满足一些条件的类别对象,你必须为它写一个单独的查询。

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.

这篇关于条件失败的左连接提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆