Tôi muốn sử dụng các bộ lọc của Hibernate, để lọc các đối tượng "MyEntity", sử dụng một lựa chọn "AnotherEntity" trong điều kiện bộ lọc. Cấu hình trông giống như sau:Làm thế nào để sử dụng thuê bao HQL trong điều kiện bộ lọc Hibernate?
<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>
nào mang lại cho tôi một org.hibernate.exception.SQLGrammarException: could not execute query
tương ứng một SQLException Table "ANOTHERENTITY" not found
, kể từ khi câu lệnh SQL được tạo ra chứa "AnotherEntity" thay vì bảng ánh xạ "ANOTHER_TABLE" như thể ánh xạ đã không được tìm thấy. Tuy nhiên, khi tôi chỉ thực hiện việc chọn một số điện thoại
select x.property1 from AnotherEntity x where property2 = :property2
nó hoạt động tốt.
Tôi bỏ lỡ điều gì ở đây? Cấu hình của tôi có sai không? Tôi có thể sử dụng Suselect HQL trong các bộ lọc không?
Tôi có cùng một vấn đề. Bạn đã tìm thấy một giải pháp? – Saffar
Tôi đã tạo câu trả lời, xem bên dưới. Tôi nghĩ bạn phải viết SQL vì HQL không được hỗ trợ. –