我想用Hibernate的過濾器來過濾「MyEntity」對象,在過濾條件中使用suselect的AnotherEntity。配置看起來是這樣的:如何在Hibernate過濾條件中使用subselect HQL?
<hibernate-mapping>
<filter-def name="myFilter" condition="someProperty in (select x.property1 from AnotherEntity x where property2 = :property2)">
<filter-param name="property2" type="long"/>
</filter-def>
<class name="com.example.MyEntity" table="SOME_TABLE">
<id name="OID" column="O_ID" type="long">
<generator class="hilo">
<param name="table">oid_id</param>
<param name="column">next_id</param>
</generator>
</id>
<version name="hibernateVersion" column="hibernate_version" unsaved-value="negative"/>
<property name="someProperty"/>
<filter name="myFilter"/>
</class>
<class name="com.example.AnotherEntity" table="ANOTHER_TABLE">
<composite-id>
<key-many-to-one name="property1" ... />
<key-many-to-one name="property2" ... />
</composite-id>
</class>
</hibernate-mapping>
這給了我一個org.hibernate.exception.SQLGrammarException: could not execute query
分別一個SQLException Table "ANOTHERENTITY" not found
,因爲生成的SQL語句包含「AnotherEntity」,而不是映射表「ANOTHER_TABLE」,彷彿映射都沒有發現。但是,當我剛執行子查詢
select x.property1 from AnotherEntity x where property2 = :property2
它只是正常工作。
我在這裏錯過了什麼?我的配置錯了嗎?我可以在過濾器中使用Suselect HQL嗎?
我有同樣的問題。你找到解決方案嗎? – Saffar
我創建了一個答案,見下文。我認爲你必須編寫SQL,因爲不支持HQL。 –