我有一个 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())。
但我只是得到一个
Caused by: org.hibernate.MappingException: collection was not an association: MyEntity.scalarSet
最佳答案
您的集合是一个集合,而不是关联映射 - 存在细微但重要的差异。当前使用 Hibernate 的 Query API 进行集合是 not supported .
您需要使用 HQL,或者通过创建具有 Long 属性的实体来使用一对多关联映射,例如:
public class Scalar {
private Long value;
public Long getValue() { .... }
public void setValue(....) { ....}
}
关于Hibernate Criteria contains-in 与表的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1303157/