Java 如何使用join fetch通过一个查询获取父实体及其子实体及其子实体
实体父项具有多个一对多关联,如下面所示。 子实体还具有一对多关联 我已成功写入检索父实体和子实体的查询:Java 如何使用join fetch通过一个查询获取父实体及其子实体及其子实体,java,hibernate,jpa,Java,Hibernate,Jpa,实体父项具有多个一对多关联,如下面所示。 子实体还具有一对多关联 我已成功写入检索父实体和子实体的查询: entityManager.createQuery("select p from Parent p " + "left join fetch p.child1 " + "left join fetch p.child2 " + "where p.parentId = :parentId", Parent.class); 执行此查询后,每隔一级子实体执行一次额外的查询,子实体的每个子实体
entityManager.createQuery("select p from Parent p " +
"left join fetch p.child1 " +
"left join fetch p.child2 " +
"where p.parentId = :parentId", Parent.class);
执行此查询后,每隔一级子实体执行一次额外的查询,子实体的每个子实体也执行一次额外的查询
是否可以在一个查询中获取包含所有子实体和子实体的子实体的父实体?
对象看起来像:
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long parentId;
\\other attributes
@OneToMany(
mappedBy = "parent",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<Child1> child1= new HashSet<>();
@OneToMany(
mappedBy = "parent",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<Child2> child2= new HashSet<>();
...
// other associations are removed because they are the same
//Constructors, getters and setters removed for brevity
}
public class Child1{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long child1Id;
\\other attributes
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Parent parent;
@OneToMany(
mappedBy = "child1",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<ChildOfChild1> childOfChild1= new HashSet<>();
//Constructors, getters and setters removed for brevity
}
public class Child2{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long child2Id;
\\other attributes
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Parent parent;
@OneToMany(
mappedBy = "child2",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<ChildOfChild2> childOfChild2= new HashSet<>();
//Constructors, getters and setters removed for brevity
}
公共类父类{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长父ID;
\\其他属性
@独身癖(
mappedBy=“parent”,
cascade=CascadeType.ALL,
孤立删除=真
)
private Set child1=新HashSet();
@独身癖(
mappedBy=“parent”,
cascade=CascadeType.ALL,
孤立删除=真
)
private Set child2=新HashSet();
...
//其他关联将被删除,因为它们是相同的
//为了简洁起见,移除了构造函数、getter和setter
}
公营儿童1{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长童;
\\其他属性
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“parent\u id”)
私人家长;
@独身癖(
mappedBy=“child1”,
cascade=CascadeType.ALL,
孤立删除=真
)
private Set childOfChild1=新HashSet();
//为了简洁起见,移除了构造函数、getter和setter
}
公营儿童2{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长童;
\\其他属性
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“parent\u id”)
私人家长;
@独身癖(
mappedBy=“child2”,
cascade=CascadeType.ALL,
孤立删除=真
)
private Set childOfChild2=new HashSet();
//为了简洁起见,移除了构造函数、getter和setter
}
您可以尝试使用以下方法:
entityManager.createQuery(“从父p中选择p”+
“左连接获取p.child1 child1\u1”+
“左联接获取child1\u0.childOfChild1”+
“左连接获取p.child2 child2_2;”+
“左联接获取child2_2;childOfChild2”+
“其中p.parentId=:parentId”,Parent.class);
看看我已经试过了,但是父实体有8个一对多的关联,所以当我实现@EntityGraphs服务时,它失败了,无法完成抓取。。