Hibernate HQL m:n join problem
Posted
by smallufo
on Stack Overflow
See other posts from Stack Overflow
or by smallufo
Published on 2010-04-20T20:03:58Z
Indexed on
2010/04/20
21:13 UTC
Read the original article
Hit count: 202
I am very unfamiliar with SQL/HQL , and am currently stuck with this 'maybe' simple problem :
I have two many-to-many Entities , with a relation table :
Car , CarProblem , and Problem .
One Car may have many Problems ,
One Problem may appear in many Cars,
CarProblem is the association table with other properties .
Now , I want to find Car(s) with specified Problem , how do I write such HQL ? All ids are Long type .
I've tried a lot of join / inner-join combinations , but all in vain..
-- updated :
Sorry , forget to mention :
Car has many CarProblem
Problem has many CarProblem
Car and Problem are not directly connected in Java Object.
-- update , java code below --
@Entity
public class Car extends Model{
@OneToMany(mappedBy="car" , cascade=CascadeType.ALL)
public Set<CarProblem> carProblems;
}
@Entity
public class CarProblem extends Model{
@ManyToOne
public Car car;
@ManyToOne
public Problem problem;
... other properties
}
@Entity
public class Problem extends Model {
other properties ...
// not link to CarProblem , It seems not related to this problem
// **This is a very stupid query , I want to get rid of it ...**
public List<Car> findCars()
{
List<CarProblem> list = CarProblem.find("from CarProblem as cp where cp.problem.id = ? ", id).fetch();
Set<Car> result = new HashSet<Car>();
for(CarProblem cp : list)
result.add(cp.car);
return new ArrayList<Car>(result);
}
}
The Model is from Play! framework , so these properties are all public .
© Stack Overflow or respective owner