我的休眠配置中有以下映射,它正在创建一个带有复合主键的表:
<hibernate-mapping>
<class name="my.package.Item" table="item"
...
<map name="itemSources" table="item_source" lazy="false">
<key>
<column name="item_id" />
</key>
<map-key-many-to-many class="my.package.SourceProperties" column="source_id"/>
<element type="text" node="externalId">
<column name="external_id"/>
</element>
</map>
...
</class>
</hibernate-mapping>
我正在尝试查询“itemSources”包含特定源 id 的所有“items”。
我尝试了几次 HQL 查询(如下所示),但均无济于事。
第一种方法
select it.id from Item it where :srcid in elements (it.itemSources)
这会导致查询映射的值,即 external_id(而不是映射的键):
... where ('4' in (select itemsour2_.external_id from item_source itemsour2_
where item1_.id=itemsour2_.item_id))
第二种方法:
select it.id from Item it where :srcid in elements (it.itemSources.source_id)
这给出了一个异常:org.hibernate.QueryException:无法取消引用标量集合元素:source_id
第三种方法:
select it.id from Item it join it.itemSources itsources where index(itsources.id) = :srcid
这给出了例外: org.hibernate.PropertyAccessException:调用 my.package.SourceProperties.id 的 getter 时发生 IllegalArgumentException
有正确的方法来实现这一目标吗?
查询地图按键:
从 Item 中选择 it.id,加入 it.itemSources 来源,其中 key(sources) =:srcid
查询地图值:
从 Item 中选择 it.id,加入 it.itemSources 源,其中sources =:externalId
简而言之:使用关键字“key”作为地图的键。地图的值不需要关键字。