java jpa/hibernate 查询返回的实体中包含的过滤器列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3300280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:11:48  来源:igfitidea点击:

Filter list contained in entity returned by jpa/hibernate query

javahibernatejpa

提问by Leonardo

I have a simple jpa entity 'ApplicationForm' with a one to many list in it:

我有一个简单的 jpa 实体“ApplicationForm”,其中有一个一对多的列表:

 @OneToMany(cascade=CascadeType.REMOVE, mappedBy="textQuestion")
 private List<Dictionary> questions;

The variable Dictionary contained in ApplicationForm is just another plain entity with just the text of the question. The corresponding database table mapped by Dictionary is:

ApplicationForm 中包含的变量 Dictionary 只是另一个简单的实体,只有问题的文本。Dictionary对应的数据库表为:

'locale' 'text'      'formId'
en       my question 123 
it       mia domanda 123

I was wondering if it's possible with jpa or hibernate, to build a query for retrieving an ApplicationForm entity with a Dictionary for a specific locale, for example 'it' only. That would be easy enough to do with standard sql, but I cannot translate in hql.

我想知道是否可以使用 jpa 或 hibernate 来构建查询以检索具有特定语言环境的字典的 ApplicationForm 实体,例如仅“它”。使用标准 sql 很容易做到,但我无法在 hql 中进行翻译。

If not possible, could you suggest an alternative way ? I have tried to manually iterate the Dictionary questions list and remove the not required locale, but is not really elegant, and also I got a jpa/hibernate error.

如果不可能,你能提出另一种方法吗?我试图手动迭代字典问题列表并删除不需要的语言环境,但不是很优雅,而且我还遇到了 jpa/hibernate 错误。

I hope I made myself clear, and code supplied is enough.

我希望我说清楚了,提供的代码就足够了。

thanks

谢谢

采纳答案by Pascal Thivent

I was wondering if it's possible with jpa or hibernate, to build a query for retrieving an ApplicationForm entity with a Dictionary for a specific locale, for example 'it' only.

我想知道是否可以使用 jpa 或 hibernate 来构建查询以检索具有特定语言环境的字典的 ApplicationForm 实体,例如仅“它”。

Not with standard JPA. But Hibernate allows to apply arbitrary filtersto a collection load during a given session. From the Hibernate Annotations Reference Guide:

不是标准 JPA。但是 Hibernate 允许在给定会话期间将任意过滤器应用于集合负载。来自 Hibernate 注释参考指南:

2.4.8. Filters

Hibernate has the ability to apply arbitrary filters on top of your data. Those filters are applied at runtime on a given session. First, you need to define them.

@org.hibernate.annotations.FilterDefor @FilterDefsdefine filter definition(s) used by filter(s) using the same name. A filter definition has a name()and an array of parameters(). A parameter will allow you to adjust the behavior of the filter at runtime. Each parameter is defined by a @ParamDefwhich has a name and a type. You can also define a defaultCondition()parameter for a given @FilterDefto set the default condition to use when none are defined in each individual @Filter. A @FilterDef(s) can be defined at the class or package level.

We now need to define the SQL filter clause applied to either the entity load or the collection load. @Filteris used and placed either on the entity or the collection element

@Entity
@FilterDef(name="minLength", parameters=@ParamDef( name="minLength", type="integer" ) )
@Filters( {
    @Filter(name="betweenLength", condition=":minLength <= length and :maxLength >= length"),
    @Filter(name="minLength", condition=":minLength <= length")
} )
public class Forest { ... }

When the collection use an association table as a relational representation, you might want to apply the filter condition to the association table itself or to the target entity table. To apply the constraint on the target entity, use the regular @Filterannotation. However, if you wan to target the association table, use the @FilterJoinTableannotation.

@OneToMany
@JoinTable
//filter on the target entity table
@Filter(name="betweenLength", condition=":minLength <= length and :maxLength >= length")
//filter on the association table
@FilterJoinTable(name="security", condition=":userlevel >= requredLevel")
public Set<Forest> getForests() { ... }

2.4.8. 过滤器

Hibernate 能够在您的数据之上应用任意过滤器。这些过滤器在运行时应用于给定会话。首先,您需要定义它们。

@org.hibernate.annotations.FilterDef@FilterDefs使用相同名称定义过滤器使用的过滤器定义。过滤器定义具有name()和 数组 parameters()。参数将允许您在运行时调整过滤器的行为。每个参数由@ParamDef具有名称和类型的 a定义。您还可以为defaultCondition()给定定义一个 参数,@FilterDef以设置在每个个体中都没有定义时使用的默认条件@Filter。A @FilterDef(s) 可以在类或包级别定义。

我们现在需要定义应用于实体加载或集合加载的 SQL 过滤器子句。@Filter被使用并放置在实体或集合元素上

@Entity
@FilterDef(name="minLength", parameters=@ParamDef( name="minLength", type="integer" ) )
@Filters( {
    @Filter(name="betweenLength", condition=":minLength <= length and :maxLength >= length"),
    @Filter(name="minLength", condition=":minLength <= length")
} )
public class Forest { ... }

当集合使用关联表作为关系表示时,您可能希望将过滤条件应用于关联表本身或目标实体表。要对目标实体应用约束,请使用常规@Filter注释。但是,如果您想以关联表为目标,请使用 @FilterJoinTable注释。

@OneToMany
@JoinTable
//filter on the target entity table
@Filter(name="betweenLength", condition=":minLength <= length and :maxLength >= length")
//filter on the association table
@FilterJoinTable(name="security", condition=":userlevel >= requredLevel")
public Set<Forest> getForests() { ... }

See also

也可以看看