我正面临一个问题,即数据正在递归调整。我想避免孩子获取父母的数据。这是造成递归问题。 我已经提到下面跳过孩子获取父级 - 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