嵌套属性对象的Hibernate + JPA @Filter

时间:2017-02-18 19:21:55

标签: java hibernate jpa

所以我理解如何将JPA / Hibernate过滤器用于直接子属性(在此处演示:annotation to filter results of a @OneToMany association)但我想要做的是使用嵌套属性来排除引用。因此,如果C作为B的引用不活动,我不希望B包含在集合中。这是一个不起作用的代码示例。它抱怨c.Active在哪里列未知。这是因为生成的sql包含c_0.is_active作为引用。有没有办法做这样的事情?

@Entity
public class A implements Serializable{
    @Id
    @Column(name = "REF")
    private int ref;

    @OneToMany
    @JoinColumn(name = "A_REF", referencedColumnName = "REF")   
    @Filter(name="test")
    private Set<B> bs;
}

@Entity
@FilterDef(name="test", defaultCondition="c.ACTIVE = 1")
public class B implements Serializable{
    @Id
    @Column(name = "A_REF")
    private int aRef;

    private C cObject;
}


@Entity
public class C implements Serializable{
    @Id
    private int ref;

    @Column(name = "ACTIVE")
    private boolean active;
}

1 个答案:

答案 0 :(得分:0)

我认为你不能以这种方式使用对其他表的引用。

试试这个:

@Entity
@FilterDef(name="test", defaultCondition="aRef = 
              (select b.aRef 
               from B b inner join C c on c.ref = b.cRef 
               where c.ACTIVE = 1)"
public class B implements Serializable{