java Hibernate 标准 JOIN + 附加条件(带子句)不适用于多对多关联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10777200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:31:36  来源:igfitidea点击:

Hibernate criteria JOIN + additional condition (with clause) don't work with many-to-many association

javahibernatecriteria

提问by Vladimir Lugovsky

I'm trying to add additional condition to Join clause using hibernate criteria. In fact, there are some methods, that allow this to do:

我正在尝试使用休眠条件向 Join 子句添加附加条件。事实上,有一些方法可以做到这一点:

createCriteria(String associationPath, String alias, int joinType, Criterion withClause)

and

createAlias(String associationPath, String alias, int joinType, Criterion withClause) 

They work properly with one-to-one and one-to-many relations. But when I'm trying to use them with entities having many-to-many relations, I'm getting following error:

它们以一对一和一对多的关系正常工作。但是,当我尝试将它们与具有多对多关系的实体一起使用时,出现以下错误:

Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.

Can anybody help me? The rude example is below:

有谁能够帮我?粗鲁的例子如下:

@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();
}
}

the Query being generated is

正在生成的查询是

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=? )

As you can see, join condition is being added two times, what is obviously incorrect

如您所见,加入条件被添加了两次,显然是不正确的

Thanks in advance.

提前致谢。

BTW I'm using postgresql 9, Hibernate 3.6.3

顺便说一句,我使用的是 postgresql 9,Hibernate 3.6.3

回答by Sergey Ponomarev

This is a bug HHH-7355 Hibernate criteria JOIN + additional condition (with clause) don't work with many-to-many associationand it will not be fixed because Hibernate Criteria API is deprecated and you should use JPA Crtiterias. You can try to use HQL withclause

这是一个错误HHH-7355 Hibernate 标准 JOIN + 附加条件(带子句)不适用于多对多关联,它不会被修复,因为 Hibernate Criteria API 已被弃用,您应该使用 JPA Crtiterias。您可以尝试使用 HQLwith子句

from Cat as cat
left join cat.kittens as kitten
    with kitten.bodyWeight > 10.0