【发布时间】:2016-06-07 13:49:56
【问题描述】:
/** 父实体**/
@Entity
@Table(name = "Parent")
public class Parent {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "parentId", unique = true, nullable = false)
private Integer parentId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
@JsonManagedReference
private Set<Child> childs = new HashSet<Child>(0);
}
****** 子实体 ******
@Entity
@Table(name = "Child")
public class Child {
private static final long serialVersionUID = 1L;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Parent.class)
@JoinColumn(name = "GuestID")
@JsonBackReference
private Parent parent;
}
当我尝试检索父详细信息时,它还会获取子记录,这不应该发生,因为我提供了 FetchType.LAZY。
*********** DAO 类 *******
public class ParentRepositoryImpl implements ParentRepository {
public final List<Parent> retrieveParentList(){
QParent qParent = QParent.parent;
JPQLQuery<Parent> query = new JPAQuery<Parent>(em);
List<Parent> parents = query.from(qParent).fetch();
}
}
我还想有条件地(根据请求)获取子记录,我该如何实现?
【问题讨论】: