1
我正在嘗試使用條件查詢來執行連接。JPA CriteriaQuery加入 - 如何加入子元素?
我的階級結構是這樣的:
@Entity
class Parent {
Intermediate intermediate;
}
@Entity
class Intermediate {
Set<Child> children
}
@Entity
class Child {
String someProperty;
}
我想獲得其中至少有一個孩子有一個匹配的屬性所有的父母,但不知道如何做加盟。如果中間對象是不存在,這將是非常容易的:
// Here, Intermediate doesn't exist - Parent has the Set<Child> property
CriteriaBuilder builder = ...
CriteriaQuery<Parent> query = ...
Root<Parent> root = query.from(Parent.class);
Join<Parent, Child> join = root.join("child");
Path path = join.get("someProperty");
Predicate predicate = builder.equal(path, "somevalue");
但隨着中間實體這樣做打破它
CriteriaBuilder builder = ...
CriteriaQuery<Parent> query = ...
Root<Parent> root = query.from(Parent.class);
Join<Parent, Child> join = root.join("intermediate.child"); <-- Fails
Caused by: java.lang.IllegalArgumentException: Unable to resolve attribute [intermediate.child] against path
at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:229) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.ejb.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:411) ~[hiberna
TE-的EntityManager-4.2.0.Final.jar: 4.2.0.Final]
我可以在這個例子中找出子對象的Path對象,但JPA似乎不想讓我使用Path對象而不是根對象來創建Join對象。
任何人都可以幫助我嗎?謝謝
啊謝謝。爲了清楚起見,我提交的代碼被剝離了大部分的JPA以清晰起見 - onetoone/manytoones存在。檢查,它很好用! – fancyplants