tags:

views:

99

answers:

2

I have two classes . for example :

class A {
public Long id ;
}
class B {
public Long id ;
public A a ;
}

B and A has many to one relationship. I can get List of B like this :

"from B b left join b.a a where a.id > 10 and b.id > 10 " . But how can I get List of A under the same join conditions ? I can't navigate A to B , Is there any solutions ? Can I do this by using criteria ?

ps : can I do it like this :

"select a from A a , B b where b.a.id = a.id " ?

A: 

be careful with

select a from A a , B b where b.a.id = a.id

I think you may find two times the table A in the from clause of the final sql query (one due to the 'A a' from the hql from clause and the second one due to b.a.id in the where clause, automatically added by hibernate. (But i'm not sure of that, you will need to check it).

Isn't

select b.a from B b left join b.a a where a.id > 10 and b.id > 10

working ?

Thierry
Yes,Thierry . I do get duplicated results by using "select a from A a , B b where b.a.id = a.id" . I need to use distinct to get the right result.I will try your suggestion . Thanks!
ZZcat
A: 

You examples should work, but have you tried:

select b.a from B b where b.id > 10 and b.a.id > 10

And yes, this should be possible with Criterias.

Willi