Hibernate - 如何强制外部加入"而不是" @过滤?

时间:2016-03-08 07:57:44

标签: java sql oracle hibernate filter

今天遇到这个问题,我无法在其他任何地方找到任何解决方案。我的应用程序中的实际实体要大得多,所以这里是我面临的问题的简化版本。我有一个有多个子实体的实体:

public class ParentEntity {
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parentEntity", cascade = CascadeType.ALL)
    @Filter(name = "childEntity_1_filter", condition="state = 'ACTIVE'")
    private Set<ChildEntity_1> childEntity_1;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parentEntity", cascade = CascadeType.ALL)
    @Filter(name = "childEntity_2_filter", condition = "entity_name not in ('x', 'y')")
    private Set<ChildEntity_2> childEntity_2;
}

现在我使用Hibernate Criteria从数据库中获取父实体。

所以我从上面指定了连接和过滤器,然后我将所有这些关联的FetchMode指定为FetchMode.JOIN

这是事情不能按预期发挥作用的地方。我的记录没有正确返回,我调试了hibernate生成的SQL。我看到了类似的东西:

# Note that this is hibernate generated sql
select
    ........
from
    parent_entity pe,
    childentity_1 ce1,
    childentity_2 ce2
where
    pe.id=ce1.id(+) and
    ce1.state (+)= 'ACTIVE' and        -- Notice the (+) here
    pe.id=ce2.id(+) and
    ce2.entity_name not in ('x', 'y'); -- Notice that (+) is missing here

正如您在上面所看到的,第一个子实体上的过滤器已将外部联接正确应用于查询,而&#34;不在&#34;过滤器没有应用此连接。

这导致我的查询在某些情况下不返回记录。例如:如果childentity_2表中存在与父实体对应的实体,但这些实体具有entity_name =&#39; x&#39;。这意味着即使连接后有记录(因为它是外连接),所有这些记录都会被not in过滤器过滤掉。

我尝试将过滤器更改为使用not equals condition:

@Filter(name = "childEntity_2_filter", condition = "entity_name != 'x' and ...")

在这种情况下,hibernate试图添加(+),但却失败了。它在(+)!之间添加了=。我尝试了<>这样的查询的各种其他组合,但无法做任何我想做的事情。

为了清楚起见,我知道如何修复查询。我的问题是如何告诉hibernate修复查询。

如何修复上述@Filter注释?我的问题是我有很多子实体都有equals @Filter并且它们都工作正常,但是在使用not in @Filter时,hibernate没有生成正确的查询。

0 个答案:

没有答案