我有一个名为'OrganizationVideo'的实体,它有一对一的实体叫做'Video',它有一个名为'OrganizationIssue'的父实体,它有一个名为published
的布尔字段
构建查询以选择OrganizationVideo
或没有任何video having OrganizationIssue.published=true
OrganziationIssues
organizationIssue is null
select ov from OrganizationVideo ov where (ov.video.organizationIssue is null OR ov.video.organizationIssue.published=true)
所以问题是当第二个OR不匹配时,此查询不返回任何内容,这意味着ov.video.organizationIssue is null
如果hibernate作为Java有效,因为第二个操作数将触发NullPointerException
生成的SQL语句:
select organizati0_.id as organizati0_.organization from organization_video organizati0_ left outer join video video1_ on organizati0_.id=video1_.id cross join organization_issue organizati2_ where video1_.organization_issue=organizati2_.id and organizati0_.organization=? and (video1_.organization_issue is null or organizati2_.published=1) order by organizati0_.id desc
答案 0 :(得分:2)
如果你想在多个地方引用ov.video
,你需要做一个显式的JOIN,然后它将这个连接用于WHERE的两个部分(而不是为每个部分做一个单独的INNER JOIN)。您可能会通过查看从查询生成的SQL来解决这个问题。
使用显式连接可以更像
SELECT ov FROM OrganizationVideo ov LEFT OUTER JOIN ov.video v LEFT OUTER JOIN v.organisationIssues oi
WHERE (oi is null OR oi.published=true)
你可以根据精确的要求使用(INNER)JOIN,但重点是相同的...通过明确指定连接,你可以保证会发生什么。