【问题标题】:Querying entities which have a specific collection entry using HQL使用 HQL 查询具有特定集合条目的实体
【发布时间】:2009-11-25 09:08:19
【问题描述】:

我有 Item 对象,它们具有定义为 ma​​p属性集合。现在我想检索所有具有特定属性的对象(namelocalevalue

Item 映射专家:

<map table="ITEM_ATTRIBUTES" name="attributes">
  <key column="item_id" foreign-key="fk_itmr_attrbts_itmrs"/>
  <composite-index class="LocalizedKey">
    <key-property name="name" column="name"/>
    <key-property name="locale" column="locale" length="32"/>
  </composite-index>
  <element column="value" type="escapedString" not-null="true"/>
</map>

此 HQL(:keyLocalizedKey:valueString

from Item item
where item.attributes[:key] = :value

不起作用并产生以下输出

error: composite-index appears in []

我通过在查询中使用纯 SQL 创建了一个解决方法。但我想知道是否有办法在 HQL 中做到这一点

我的普通 sql 解决方法:

select i.* from items i
left join item_attributes a on a.item_id = i.id
where a.name = :name
and a.locale = :locale
and a.value = :value

【问题讨论】:

    标签: sql hibernate collections hql


    【解决方案1】:

    文档提示 index() 运算符应该从您的地图中返回您的密钥。鉴于您的键是一个复合对象,我不完全确定您将能够在查询中使用它做什么。很抱歉在给您建议之前无法对此进行测试,但我想看看您会得到什么:

    from Item item join item.attributes attr where attr.index().name = :name 
           and attr.index().locale = :locale and attr.value = :value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2012-04-17
      • 2021-09-15
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多