Hibernate过滤儿童 [英] Hibernate filter children

查看:117
本文介绍了Hibernate过滤儿童的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

如何为作用于子级的父实体类指定一个过滤器?

解方案

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

并确定您启用了过滤器...


I have two tables one being the parent of the other. The child table has start date and end date columns, which determine its validity. The following filter setup works for loading only the valid children:

@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 {}

What I want is to load all the parents and only the valid children starting with the parent entity definition. The following code loads all the children, not only the valid ones:

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

How do I specify a filter for the parent entity class that acts on the children?

解决方案

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

And make sure you enable the filter...

这篇关于Hibernate过滤儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆