我正面臨一個問題,即數據正在遞歸調整。我想避免孩子獲取父母的數據。這是造成遞歸問題。 我已經提到下面跳過孩子獲取父級 - JPA
POJO的結構
class Parent {
..
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private List<Child> childs;
..
}
class Child {
..
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentId")
private Parent parent;
..
}
代碼獲取數據這樣
` em = EMF.get().createEntityManager();
Query q = em.createQuery("Select p from Parent p", Parent.class);
List<Parent> parents = q.getResultList();
// Till this point all looks good but when the code gets executed
parent.getChilds();
`
它取這樣的數據:
Parent
child1
Parent
child2
Parent
child2
Parent
..
..
child2
..
哪個我不需要我只是想要這樣的數據:
Parent1
child1
child2
Parent2
child1
child2
child3
默認情況下,默認的獲取模式是懶惰的 – soorapadman
@soorapadman默認情況下,獲取對於多對象是懶惰的,並且渴望對一個關係 – crizzis
什麼提供者? OneToOne/ManyToOne的延遲獲取是提供程序特定的,可能需要更多取決於您的環境的內容。例如,EclipseLink需要編織https://www.eclipse.org/eclipselink/documentation/2.5/concepts/app_dev007.htm – Chris