-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: traversing associations in composite pk using criteria
PostPosted: Mon Feb 01, 2010 6:35 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
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 10g


What's the correct way to travers such a relationship?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.