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

使用HQL查询具有特定集合项的实体

  •  2
  • naltatis  · 技术社区  · 16 年前

    Item 具有 属性集合 地图 name , locale 和 value )

    一笔费用 映射:

    <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(带有 :key LocalizedKey 和 :value 作为 String

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

    无法工作并产生以下输出

    error: composite-index appears in []
    

    我在查询中使用纯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
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   BryanD    16 年前

    文档提示index()操作符应该从映射返回密钥。考虑到您的键是一个复合对象,我不完全确定您在查询中能用它做什么。很抱歉,在给你建议之前,我没能把这个测试出来,但我试着看看你能得到什么:

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