JPA过滤器实体嵌套的对象列表

时间:2014-09-08 11:05:06

标签: jpa

我正在使用JPA 2.1。我有3个实体:Dr01Dr02Dr03,结构如下:

public class Dr01 implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dr01")
    private List<Dr02> dr02List;

}
public class Dr02 implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dr02")
    private List<Dr03> dr03List;

    @JoinColumn(name = "DR2CLM", referencedColumnName = "DR1CLM", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Dr01 dr01;

}    

public class Dr03 implements Serializable {

    @JoinColumns({
        @JoinColumn(name = "DR3CLM", referencedColumnName = "DR2CLM", insertable = false, updatable = false),
        @JoinColumn(name = "DR3PTFN", referencedColumnName = "DR2PTFN", insertable = false, updatable = false)})
    @ManyToOne(optional = false)
    private Dr02 dr02;

    private elementOBJ element;
}

public class elementOBJ implements Serializable {

    @Column(name = "XXX")
    private int id;

    @Column(name = "YYY")
    private int status;
}

我想从Dr01中进行选择,只获取状态字段中包含值为1的元素对象的Dr03个对象。

如何检索由其状态值过滤的dr03List? (在选择之后不过滤。)

感谢提前。

1 个答案:

答案 0 :(得分:0)

可能有帮助的选项:

  1. 根据DR03表的状态创建数据库视图并映射您的 那个实体。
  2. 使用状态作为JPA继承 DiscriminatorColumn。
  3. 如果使用Hibernate,请使用非JPA @Where 过滤集合的注释