Hibernate过滤子项

时间:2012-07-23 19:28:15

标签: java hibernate

我有两张桌子,一张是另一张桌子的父母。子表具有开始日期和结束日期列,用于确定其有效性。 以下过滤器设置仅适用于加载有效的子项:

@Entity
@Table(name = "child")
@FilterDef(name = "dateFilter", parameters = @ParamDef( name = "targetDate", type = "string" ))
@Filter(name = "dateFilter", condition = "(:targetDate between to_char(startDate, 'yyyyMMdd') and to_char(endDate, 'yyyyMMdd')")
public class Child {}

我想要的是加载所有父项,只加载以父实体定义开头的有效子项。以下代码加载所有子代,而不仅仅是有效代码:

@Entity
@Table(name = "parent")
public class Parent {
    @OneToMany(mappedBy = "parent")
    private Collection<Child> childCollection = new HashSet<Child>();
}    

如何为作用于子节点的父实体类指定过滤器?

1 个答案:

答案 0 :(得分:1)

@Entity
@Table(name = "parent")
@FilterDef(name = "dateFilter", parameters = @ParamDef( name = "targetDate", type = "string" ))
public class Parent {
    @OneToMany(mappedBy = "parent")
    @Filter(name = "dateFilter", condition = "(:targetDate between to_char(startDate, 'yyyyMMdd') and to_char(endDate, 'yyyyMMdd')")
    private Collection<Child> childCollection = new HashSet<Child>();
} 

并确保启用过滤器...