【发布时间】:2011-07-25 10:35:38
【问题描述】:
我想使用 Hibernate 的过滤器来过滤“MyEntity”对象,在过滤条件中使用“AnotherEntity”的 suselect。配置如下所示:
<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 吗?
【问题讨论】:
-
我也有同样的问题。你找到解决办法了吗?
-
我创建了一个答案,见下文。我认为您必须编写 SQL,因为不支持 HQL。