代码之家  ›  专栏  ›  技术社区  ›  Leonardo

jpa/hibernate查询返回的实体中包含的筛选器列表

  •  8
  • Leonardo  · 技术社区  · 14 年前

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

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

    ApplicationForm中包含的变量字典只是另一个简单的实体,只有问题的文本。

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

    我想知道是否有可能使用jpa或hibernate,构建一个查询来检索ApplicationForm实体,其中包含一个特定语言环境的字典,例如“it”。 用标准sql很容易做到,但我不能用hql进行翻译。

    如果不可能,你能建议另一种方法吗?我曾尝试手动迭代字典问题列表并删除不需要的区域设置,但这并不是很优雅,而且我还遇到了一个jpa/hibernate错误。

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

    1 回复  |  直到 14 年前
        1
  •  7
  •   Pascal Thivent    14 年前

    我想知道是否有可能使用jpa或hibernate,构建一个查询来检索ApplicationForm实体,其中包含一个特定语言环境的字典,例如“it”。

    filters

    2.4.8. Filters

    Hibernate具有应用 在你的数据上的任意过滤器。 在给定的会话中。首先,你需要

    @org.hibernate.annotations.FilterDef @FilterDefs 筛选器使用的定义 相同的名字。筛选器定义已存在 一 name() parameters() . 参数将允许 你需要调整 运行时筛选。每个参数都是 定义为 @ParamDef 名称和类型。您还可以定义 defaultCondition() 鉴于 @FilterDef 未定义时使用的条件 @Filter . A @过滤器def (s) 可以在 类或包级别。

    适用于实体的条款 加载或集合加载。 @过滤器 使用并放置在 实体或集合元素

    @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 { ... }
    

    当集合使用关联时 表作为关系表示, 您可能需要应用筛选器 自身或目标实体表。 实体,使用常规 @过滤器 以关联表为目标,使用 @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() { ... }