我尝试使用hibernate @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时使用hibernate注释是错误的,那么我的问题的解决方案是什么呢?
谢谢。
发布于 2018-04-07 14:55:49
@Filter将仅在同一实体上进行过滤。如果你想过滤连接表中的数据,你必须使用@FilterJoinTable。
Eg: @FilterJoinTable(name="column_name", condition="your_condition")https://stackoverflow.com/questions/49695064
复制相似问题