[英]Hibernate criteria JOIN + additional condition (with clause) don't work with many-to-many association
我正在尝试使用hibernate条件为Join子句添加附加条件。 事实上,有一些方法允许这样做:
createCriteria(String associationPath, String alias, int joinType, Criterion withClause)
和
createAlias(String associationPath, String alias, int joinType, Criterion withClause)
他们通过一对一和一对多的关系正常工作。 但是当我尝试将它们与具有多对多关系的实体一起使用时,我会遇到以下错误:
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.
有谁能够帮我? 粗鲁的例子如下:
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
@ManyToMany
private Set<PersonName> names;
}
public class PersonName {
@Id
@GeneratedValue
private Long id;
@Column
private String name;
}
public class PersonDao extends HibernateDaoSupport {
public List<Person> findByName() {
Criteria criteria = getSession().createCriteria(Person.class, "p");
criteria.createCriteria("p.names", "names", JoinType.INNER_JOIN, Restrictions.eq("name", "John"));
return criteria.list();
}
}
生成的查询是
select this_.id as y0_ from person this_
inner join debtor_info this_1_ on this_.id=this_1_.id
left outer join person_person_name personname3_ on this_.id=personname3_.person_id and ( name1_.name=? )
left outer join person_name name1_ on personname3_.person_name_id=name1_.id and ( name1_.name=? )
如您所见,连接条件正在添加两次,这显然是不正确的
提前致谢。
顺便说一下,我正在使用postgresql 9,Hibernate 3.6.3
这是一个错误HHH-7355 Hibernate标准JOIN +附加条件(带子句)不适用于多对多关联,并且它不会被修复,因为Hibernate Criteria API已被弃用,您应该使用JPA Crtiterias。 您可以尝试使用HQL with
子句
from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight > 10.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.