加入map并在HQL中引用它的键/值

时间:2010-06-11 06:23:17

标签: hibernate map hql

假设我有一张地图:

    <map name="externalIds" table="album_external_ids">
        <key column="album_id" not-null="true"/>
        <map-key-many-to-many class="Major" column="major_id"/>
        <element column="external_id" type="string" not-null="true"/>
    </map> 

如何使HQL意味着“选择地图键的id ==:foo和map value ==:bar的实体”?

我可以使用select album from Album album join album.externalIds ids加入 但是,我如何才能引用ids的关键和价值呢? ids.key.id =:foo和ids.value =:bar不起作用,hibernate doc对此主题保持沉默。

天真的方法不起作用:

select album from Album album join album.externalIds externalId where index(externalId).id = :foo and externalId = :bar

select album from Album album join album.externalIds externalId join index(externalId) major where major.id = :foo and externalId = :bar

1 个答案:

答案 0 :(得分:4)

我相信您的查询应如下所示:

select album from Album album where album.externalIds['foo'] = 'bar'

希望有所帮助,

VincentGiguère