用于过滤 @OneToMany 关联结果的注释 [英] annotation to filter results of a @OneToMany association

查看:38
本文介绍了用于过滤 @OneToMany 关联结果的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表之间的父/子关系,以及我的 Java 类中的相应映射.表格大致如下所示:

I have parent/child relationship between two tables, and the corresponding mapping in my Java classes. The tables roughly look like that:

A (ref number, stuff varchar2(4000))
B (a_ref number, other 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;
}

这很好用,但我想在我从子表中检索的行上添加一个过滤器.生成的查询如下所示:

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:

select a_ref, other, foo from B where a_ref = ?

我希望它是:

select a_ref, other, foo from B where a_ref = ? and other = 123

附加过滤器将只是一个列名和一个硬编码值.有没有办法使用休眠注释来做到这一点?

The additional filter would be only a column name and a hard-coded value. Is there a way to do that using hibernate annotations?

我查看了 @JoinFormula,但是我总是必须从父表中引用一个列名(在 JoinFormula 的 name 属性中).

I've looked at @JoinFormula, but with that I always have to reference a column name from the parent table (in the name attribute of the JoinFormula).

提前感谢您的建议.

推荐答案

JPA 不支持它,但是如果您使用 hibernate 作为 JPA 提供程序,那么您可以使用注释 @FilterDef@Filter.

It is not supported by JPA but if you are using hibernate as JPA provider then you can use annotation @FilterDef and @Filter.

Hibernate 核心参考文档

Hibernate3 能够预定义过滤条件并附加类级别和集合级别的过滤器.一个过滤器标准允许您定义类似于类和各种可用的现有where"属性集合元素.然而,这些过滤条件可以是参数化.然后应用程序可以在运行时决定是否应启用某些过滤器及其参数值应该.过滤器可以像数据库视图一样使用,但它们是在应用程序内部参数化.

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.

示例

@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

这篇关于用于过滤 @OneToMany 关联结果的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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