Hibernate Criteria contains-in 与表的关联
我有一个看起来像这样的 Hibernate 映射:
<class name="MyEntity">
<set name="scalarSet" table="(select fk, scalar_value from other_table)">
<key column="fk"/>
<property column="scalar_value" type="long"/>
</set>
</class
鉴于此,我如何查询以便 MyEntity.scalarSet (即 Set)的值位于另一个集合中。
类似于:
criteria.add(Restrictions.in("scalarSet", targetList));
[编辑] 我也尝试过 Restriction.sqlRestriction(..)。 我使用的sql查询是这样的:
"1 == (select fk, scalar_value from other_table where fk = {alias}.id and scalar_value in ({expanding?})"
其中'{expanding?}'被逗号分隔的问号替换(取决于targetList.size())。
但我刚刚得到一个
由以下原因引起:org.hibernate.MappingException:集合不是关联:MyEntity.scalarSet
I have a Hibernate mapping that looks something like this:
<class name="MyEntity">
<set name="scalarSet" table="(select fk, scalar_value from other_table)">
<key column="fk"/>
<property column="scalar_value" type="long"/>
</set>
</class
Given this, how do I query such that a value of MyEntity.scalarSet (which is Set) is in a another collection.
Something like:
criteria.add(Restrictions.in("scalarSet", targetList));
[edit]
I've also tried Restriction.sqlRestriction(..). The sql query that I used is something like this:
"1 == (select fk, scalar_value from other_table where fk = {alias}.id and scalar_value in ({expanding?})"
Wherein '{expanding?}' is replaced by comma-separated question marks (depending on targetList.size()).
But I'm just getting a
Caused by: org.hibernate.MappingException: collection was not an association: MyEntity.scalarSet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的集合是一个集合,而不是关联映射 - 存在细微但重要的差异。 当前不支持使用 Hibernate 的集合查询 API。
您需要使用 HQL,或者通过创建具有 Long 属性的实体来使用一对多关联映射,例如:
Your set is a collection, not an association mapping - there are subtle but important differences. Using Hibernate's Query API for collections is currently not supported.
You need to either use HQL, or use a one-to-many association mapping by creating an entity with Long property, for example:
然而,有人编写了一个补丁,它可能或可能不会出现在 Hibernate 3.4 中
However, someone has written a patch which may or may not end up in Hibernate 3.4