休眠标准包含在与表的关联中 [英] Hibernate Criteria contains-in on an association to a table
问题描述
我有一个类似于以下的Hibernate映射:
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
鉴于此,如何查询MyEntity的值.scalarSet(已设置为Set)在另一个集合中。
Given this, how do I query such that a value of MyEntity.scalarSet (which is Set) is in a another collection.
类似的东西:
criteria.add(Restrictions.in("scalarSet", targetList));
[edit]
我也尝试过Restriction.sqlRestriction(..)。我使用的sql查询如下所示:
[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?})"
其中'{expanding?}'替换为逗号分隔的问号(取决于targetList.size())。
Wherein '{expanding?}' is replaced by comma-separated question marks (depending on targetList.size()).
但是我只是得到
原因:org.hibernate.MappingException:集合不是关联:MyEntity.scalarSet
Caused by: org.hibernate.MappingException: collection was not an association: MyEntity.scalarSet
推荐答案
您的集合是一个集合,而不是关联映射-有一些细微但重要的区别。使用Hibernate的Query API进行集合目前不支持。
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.
您需要使用HQL或使用一个通过创建具有Long属性的实体进行多对多关联映射,例如:
You need to either use HQL, or use a one-to-many association mapping by creating an entity with Long property, for example:
public class Scalar {
private Long value;
public Long getValue() { .... }
public void setValue(....) { ....}
}
这篇关于休眠标准包含在与表的关联中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!