Hibernate Criteria contains-in on an association to a table

爷,独闯天下 提交于 2020-01-03 19:08:29

问题


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


回答1:


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:

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



回答2:


However, someone has written a patch which may or may not end up in Hibernate 3.4



来源:https://stackoverflow.com/questions/1303157/hibernate-criteria-contains-in-on-an-association-to-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!