休眠与MappedSuperClass相关表过滤器 [英] Hibernate Filters on related table with MappedSuperClass

查看:152
本文介绍了休眠与MappedSuperClass相关表过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想筛选,只显示激活的实体,同时使查询,使用过滤器和一个基地MappedSuperClass运行到哪里关联的表类不尊重相同的过滤器的问题。

I'm trying to filter to only show "Active" entities while making a query, using filters and a base MappedSuperClass and running into problems where the associated table classes aren't respecting the same filter.

下面是我的模型设置:

@FilterDef(name="baseStatus")
@Filter(name="baseStatus", condition= "Status = 'A'")
@MappedSuperclass
public abstract class BaseModel {

    @Column(name = "Status", insertable = false)
    private String status;

    // getters and setters...
}

我也有具有相关的集合模型,例如..

I also have models that have related collections, eg..

@Entity
@Table(name = "A")
public class A extends BaseModel {
    @OneToMany()
    private List<B> bs = new ArrayList<B>(0);
// other model properties
}

@Entity
@Table(name = "B")
public class B extends BaseModel {
// model properties
}

在查询之前,我运行:

Before querying, I run:

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(A.class)
Filter filter = getCurrentSession().enableFilter("baseStatus");
List<A> as = criteria.list();

使用此设置,我得到适用于我查询的主类适当的过滤器,但孩子集合(BS)没有得到过滤的加入,让我留下了唯一的活动A与B,有些地方的BS是不活跃的一个子集。

Using this setup I get the proper filter applied to the main class I am querying, but the child collection ("bs") doesn't get filtered on the join, so I'm left with only active "A" with a child collection of "B", where some of the "B"s are inactive.

在理想情况下,我想只有对BaseModel过滤器。有没有办法做我正在寻找不上的每个关联应用单独的过滤器?

Ideally I'd like to only have the filter on the BaseModel. Is there any way to do what I'm looking for without applying a separate filter on each association?

谢谢!

推荐答案

我找不到在结束了一个很好的解决方案,无需定制构建的东西,所以我结束了在每一个模型在每个关联表注释@Filter如下

I couldn't find a great solution for this at the end without building something custom so I ended up annotating @Filter at each association table in each model as follows

@FilterDef(name="baseStatus")
@Filter(name="baseStatus", condition= "Status = 'A'")
@MappedSuperclass
public abstract class BaseModel {

    @Column(name = "Status", insertable = false)
    private String status;

    // getters and setters...
}

@Entity
@Table(name = "A")
public class A extends BaseModel {
    @OneToMany()
    @Filter(name="baseStatus", condition= "Status = 'A'")
    private List<B> bs = new ArrayList<B>(0);
// other model properties
}

@Entity
@Table(name = "B")
public class B extends BaseModel {
// model properties
}

虽然不理想,它的工作原理,我仍然只需要调用

While not ideal, it works and I still only need to call

Filter filter = getCurrentSession().enableFilter("baseStatus");

在一个共同的数据访问层一次。

once in a common data access layer.

这篇关于休眠与MappedSuperClass相关表过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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