Hibernate如何忽略@Where注释 [英] Hibernate how to ignore @Where annotation

查看:108
本文介绍了Hibernate如何忽略@Where注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体:

  @Entity 
@Table(name =[Usermaster])
@Where(clause =isDeleted = 0)
public class User {
// ...
}

在某些流程中,我需要忽略@Where注释并获取用户,即使isDeleted不为0。 (我使用CRUD库进行查询)

解决方案有一个动态版本的 @Where

code>设置,它是 @Filter 。请参阅:





Hibernate有能力预先定义过滤条件并附加这些过滤器无论是课堂级别还是集合级别。过滤条件允许您定义一个类似于类和各种集合元素上现有的其中属性的限制条款。


@Filter的管理更简单一些:

<需要定义过滤器


  • < filter-def> / @FilterDef / li>
  • < filter> / @Filter 必须分配给class或a set

  • 过滤器必须在会话级别启用,例如: session.enableFilter(myFilter)。setParameter(myFilterParam,some-value) ;



所以,这虽然稍微复杂一点,但它提供了我们需要的东西:dynamic @Where 在运行时打开/关闭


I have an entity:

@Entity
@Table(name = "[Usermaster]")
@Where(clause = "isDeleted = 0")
public class User {
//...
}

in some flow I need to ignore @Where annotation and get user even if isDeleted is not 0. How can I make it? (I use CRUD repositories to query)

解决方案

There is a dynamic version of the @Where setting, it is the @Filter. See:

Hibernate 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.

Management of @Filter is a bit more complex, in a nutshell:

  • <filter-def> / @FilterDef is needed to define filter
  • <filter> / @Filter must be assigned to class or a set
  • filter must be enabled on a session level, e.g.: session.enableFilter("myFilter").setParameter("myFilterParam", "some-value");

So, this, while being a bit more complex, provides exactly what we need: dynamic @Where to be turned on/off in run-time

这篇关于Hibernate如何忽略@Where注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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