问题
I'm trying to do a nested join on a self referencing entity, please bear with me, it will make sense :)
Thing.java
class Thing {
Integer something;
Person owner;
}
Person.java
class Person {
String name;
Person parent;
}
Note: A Person with parent=null means that they are a parent, and cannot have a parent. It's an alien world! :P
Query:
Root<Thing> root = query.from(Thing.class);
Join<Thing, Person> ownerJoin = root.join(Thing_.owner);
Join<Person, Person> parentJoin = ownerJoin.join(Person_.parent);
Path<String> ownerNameField = ownerJoin.get(Person_.name);
Path<String> parentNameField = parentJoin.get(Person_.name);
return cb.or(cb.equal(ownerNameField, "John"), cb.equal(parentNameField, "John"));
So basically, I'm trying to find "Things" that either belong to me, or that I'm the parent of the Person who owns that "Thing".
The SQL statement I'm trying to generate is supposed to look something like this:
SELECT * FROM Thing t
WHERE
t.something = 1
AND (t.owner.name = "John" OR t.owner.parent.name = "John")
Now the problem is that with this criteria it's only giving me the results of t.owner.parent.name = "John"
来源:https://stackoverflow.com/questions/26669841/java-criteria-api-join-self-referencing-entity