Java Criteria API join self referencing entity

自古美人都是妖i 提交于 2019-12-25 11:27:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!