2012-07-23 73 views
0

我有两个表格,一个是另一个的父表格。子表具有开始日期和结束日期列,这些列确定其有效性。 以下过滤器的设置适用于仅加载有效的小孩:休眠筛选器儿童

@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
@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>(); 
} 

并确保您启用过滤器...

+0

的@FilterDef注释只能用于该类接受,但这种修正它的工作原理。我修改了您的答案,但“只有在您进行同行评审之前,您才能看到此修改。”无论如何,谢谢! – lbalazscs 2012-07-23 20:31:57

+1

是的,你是正确的,它不被接受的方法/领域。但是,为了完全准确,它不仅限于TYPE(Class);你也可以在PACKAGE上定义它。注意到你编辑已被某人拒绝,我会更新它(我认为我可以自己...) – 2012-07-24 02:33:03