使用 MappedSuperClass 在相关表上休眠过滤器 [英] Hibernate Filters on related table with MappedSuperClass

查看:24
本文介绍了使用 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
}

在查询之前,我运行:

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

使用此设置,我将正确的过滤器应用于我正在查询的主类,但子集合(bs")不会在连接时过滤,因此我只剩下活动的A"B"的子集合,其中一些B"处于非活动状态.

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天全站免登陆