簡體   English   中英

Hibernate標准JOIN +附加條件(帶子句)不適用於多對多關聯

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM