注释来过滤@OneToMany关联的结果 [英] annotation to filter results of a @OneToMany association
问题描述
我有两个表之间的父/子关系,以及我的Java类中的相应映射。这些表大致看起来像这样:
$ $ p $ code> A(ref number,stuff varchar2(4000))
B(a_ref number ,其他数字,foo varchar2(200))
和Java代码:
@Entity
class A {
@Id
@Column(name =REF)
private int ref;
@OneToMany
@JoinColumn(name =A_REF,referencedName =REF)
private Set< B> BS;
}
@Entity
class B {
@Id
@Column(name =A_REF)
private int aRef;
@Id
@Column(name =OTHER)
private int other;
}
这很好,但我想在行上添加一个过滤器我从子表中检索。生成的查询如下所示:
select a_ref,other,foo from B where a_ref =?
我希望它是:
从B选择a_ref,other,foo其中a_ref =?和其他= 123
额外的过滤器只是一个列名和一个硬编码值。有没有办法做到这一点使用hibernate注释?
我已经看过 @JoinFormula
,但随着我总是必须引用父表中的列名(在JoinFormula的 name
属性中)。
感谢您提供任何建议。 JPA不支持它,但如果您使用hibernate作为JPA提供程序,那么您可以使用注释 @FilterDef
和 @Filter
。
Hibernate3能够预先定义过滤条件并在类级别和集合级别附加
这些过滤器。过滤器
条件允许您定义一个限制条款,类似于该类上可用的
现有where属性和各种
集合元素。但是,这些过滤条件可以是
参数化的。然后,应用程序可以在运行时决定是否应启用
某些过滤器以及它们的参数值
应该是什么。过滤器可以像数据库视图一样使用,但它们在应用程序内部被
参数化。
示例 I have parent/child relationship between two tables, and the corresponding mapping in my Java classes. The tables roughly look like that: and the Java code: This works fine, but I'd like to add a filter on the rows that I retrieve from the child table. The query that is generated looks like: And I'd like it to be: The additional filter would be only a column name and a hard-coded value. Is there a way to do that using hibernate annotations? I've looked at Thanks in advance for any advice. It is not supported by JPA but if you are using hibernate as JPA provider then you can use annotation Hibernate Core Reference Documentation Hibernate3 has the ability to pre-define filter criteria and attach
those filters at both a class level and a collection level. A filter
criteria allows you to define a restriction clause similar to the
existing "where" attribute available on the class and various
collection elements. These filter conditions, however, can be
parameterized. The application can then decide at runtime whether
certain filters should be enabled and what their parameter values
should be. Filters can be used like database views, but they are
parameterized inside the application. Exemple
这篇关于注释来过滤@OneToMany关联的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ $ $
$ public class A实现Serializable {
@Id
@Column( name =REF)
private int ref;
@OneToMany
@JoinColumn(name =A_REF,referencedColumnName =REF)
@Filter(name =test)
private设置< B> ; BS;
$ b $ @实体
@FilterDef(name =test,defaultCondition =other = 123)
public class B实现Serializable {
@I $
@Column(name =A_REF)
private int aRef;
@Id
@Column(name =OTHER)
private int other;
}
会话会话= entityManager.unwrap(Session.class);
session.enableFilter(test);
a a = entityManager.find(A.class,new Integer(0))
a.getb()。size()//只包含等于123的b
A (ref number, stuff varchar2(4000))
B (a_ref number, other number, foo varchar2(200))
@Entity
class A {
@Id
@Column(name = "REF")
private int ref;
@OneToMany
@JoinColumn(name = "A_REF", referencedName = "REF")
private Set<B> bs;
}
@Entity
class B {
@Id
@Column(name = "A_REF")
private int aRef;
@Id
@Column(name = "OTHER")
private int other;
}
select a_ref, other, foo from B where a_ref = ?
select a_ref, other, foo from B where a_ref = ? and other = 123
@JoinFormula
, but with that I always have to reference a column name from the parent table (in the name
attribute of the JoinFormula).@FilterDef
and @Filter
.
@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="other = 123")
public class B implements Serializable{
@Id
@Column(name = "A_REF")
private int aRef;
@Id
@Column(name = "OTHER")
private int other;
}
Session session = entityManager.unwrap(Session.class);
session.enableFilter("test");
A a = entityManager.find(A.class, new Integer(0))
a.getb().size() //Only contains b that are equals to 123