Hibernate条件包含在与表的关联中

Hibernate条件包含在与表的关联中,hibernate,criteria,contains,scalar,Hibernate,Criteria,Contains,Scalar,我有一个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 "1

我有一个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
"1 == (select fk, scalar_value from other_table where fk = {alias}.id and scalar_value in ({expanding?})"
[编辑] 我还尝试了Restriction.sqlRestriction(..)。我使用的sql查询如下所示:

<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
"1 == (select fk, scalar_value from other_table where fk = {alias}.id and scalar_value in ({expanding?})"
其中,{expansing?}由逗号分隔的问号替换(取决于targetList.size()

但我只是得到了一份工作

原因:org.hibernate.MappingException:集合不是关联:MyEntity.scalarSet


您的集合是一个集合,而不是一个关联映射-存在细微但重要的差异。目前正在为集合使用Hibernate的查询API

您需要使用HQL,或者通过创建具有Long属性的实体来使用一对多关联映射,例如:

public class Scalar {
  private Long value;
  public Long getValue() { .... }
  public void setValue(....) { ....}
}

然而,有人写了一篇文章,可能会也可能不会出现在Hibernate 3.4中

谢谢,在浏览了Hibernate的源代码之后,这就是我最后要做的:-)