【问题标题】:Filtering the @OneToMany collection过滤 @OneToMany 集合
【发布时间】:2018-04-06 14:27:23
【问题描述】:

我正在尝试使用休眠@Filter 注释来过滤@ManyToOne 集合的返回值。因为该应用程序使用“软删除”(即状态从活动变为已删除),所以我想过滤掉“已删除”。

基本类设置:

@Entity
@FilterDef(name = "active", defaultCondition = "status = 'A'")
public class Foo {
    private Status status;

    // setters and getters
}

@Entity
public class Bar {
    @OneToMany(mappedBy = "bar")
    @Filter(name = "active")
    private List<Foo> fooList;

    // setters and getters
}

我使用CrudRepository 来检索Bar 对象:

public interface BarDao extends CrudRepository<Bar, Long> {
    Bar getById(Long id);
}

如果我在数据库中有 1 个 Bar 对象和 3 个关联的 Foo 对象。 2 为“活动”,1 为“已删除”。但是,当我在 CrudRepository 上运行查询时,集合包含所有三个并且 @Filter 尚未被调用。

使用CrudRepository 时可能使用休眠注释是错误的,那么我的问题的解决方案是什么。

谢谢。

【问题讨论】:

  • 为什么 ManyToOne 使用 List 而不仅仅是 Foo
  • 因为我写错了例子,所以更新了抱歉!

标签: spring hibernate crud


【解决方案1】:

@Filter 只会对同一实体进行过滤。如果你想过滤连接表的数据,你必须使用@FilterJoinTable

Eg: @FilterJoinTable(name="column_name", condition="your_condition")

【讨论】:

  • 抱歉没用,因为这不是通过连接表连接的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 2014-03-12
  • 1970-01-01
  • 2016-08-18
相关资源
最近更新 更多