由jpa / hibernate查询返回的实体中包含的过滤器列表 [英] Filter list contained in entity returned by jpa/hibernate query

查看:298
本文介绍了由jpa / hibernate查询返回的实体中包含的过滤器列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @OneToMany(cascade = 1)

我有一个简单的jpa实体'ApplicationForm' CascadeType.REMOVE,mappedBy =textQuestion)
private List< Dictionary>问题;

ApplicationForm中包含的变量Dictionary仅仅是另一个只包含问题文本的简单实体。
由Dictionary映射的相应数据库表为:

 'locale''text''formId'
en我的问题123
it mia domanda 123

我想知道是否可以使用jpa或休眠,建立一个查询来检索一个特定语言环境的Dictionary的ApplicationForm实体,例如'it'。
这对使用标准sql来说很容易,但我无法在hql中进行翻译。



如果不行,您能否提出一种替代方法?我试图手动迭代字典问题列表并删除不需要的区域设置,但不是很优雅,而且我得到了jpa / hibernate错误。



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



谢谢 解决方案

blockquote>

我想知道是否可以使用jpa或hibernate来构建一个查询来检索带有特定语言环境的Dictionary的ApplicationForm实体,例如只有'it'。

不符合标准的JPA。但Hibernate允许应用任意过滤器在给定会话期间收集负载。从Hibernate注解参考指南:


2.4.8。过滤器



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



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



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



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

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

  @OneToMany 
@JoinTable
//过滤目标实体表
@Filter(name =betweenLength,condition =:minLength < = length和:maxLength> = length)
//在关联表上过滤
@FilterJoinTable(name =security,condition =:userlevel> = requredLevel)
public Set< Forest> getForests(){...}




另见




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

 @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:

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

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.

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

thanks

解决方案

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.

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

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.FilterDef or @FilterDefs define 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 @ParamDef which has a name and a type. You can also define a defaultCondition() parameter for a given @FilterDef to 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. @Filter is 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 @Filter annotation. However, if you wan to target the association table, use the @FilterJoinTable annotation.

@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

这篇关于由jpa / hibernate查询返回的实体中包含的过滤器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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