Vladimir Lugovsky
Vladimir Lugovsky

Reputation: 197

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

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

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

Upvotes: 4

Views: 12448

Answers (1)

Sergey Ponomarev
Sergey Ponomarev

Reputation: 3181

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

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

Upvotes: 4

Related Questions