Hi,
I'm trying to write a criteria query that traverses a composite primary key in an association class, but hibernate does not create the join necessary for the second association.
There are 3 classes involved: A, A2B and B. A2B is an association class with a composite primary key with @ManyToOne associations to A and B.
Here's how the classes look:
Code:
class A{
[..]
@OneToMany
Set<A2B> assoc2b;
[..]
}
Code:
class B{
private String value;
public String getValue(){
return value;
}
public setValue(String str){
this.value = str;
}
[..]
@OneToMany
Set<A2B> assoc2a;
[..]
}
and the association class:
Code:
class A2B{
[..]
public static class A_TO_B_ID implements Serializable {
@ManyToOne
private A a;
@ManyToOne
private B b;
}
public A getA(){
return a;
}
public setA(A a){
this.a = a;
}
private String str;
public String getStr(){
return str;
}
public void setStr(String str){
this.str = str;
}
}
[..]
@EmbeddedId
private A_TO_B_ID pk;
@Transient
public A getA(){
return pk.a;
}
public void setA(A a){
this.pk.a = a;
}
}
This criteria query works:
Code:
List l = vs.createCriteria( A.class ).createAlias( "assoc2b","toB").add( Restrictions.eq( "pk.str", "test" ) ).list();
but this 1.:
Code:
List l = vs.createCriteria( A.class ).createAlias( "assoc2b","toB").createAlias( "toB.pk","p").createAlias( "p.b","b").add( Restrictions.eq( "b.value", "test" ) ).list();
or this 2.:
Code:
List l = vs.createCriteria( A.class ).createAlias( "assoc2b","toB").createAlias( "toB.pk.b","b").add( Restrictions.eq( "b.value", "test" ) ).list();
do not.
Both throws an oracle exception because the "b" join was not built, i.e. it is not defined in the sql statement.
Hibernate 3.3 CR1
Oracle 10gWhat's the correct way to travers such a relationship?