Java 在所有嵌套实体中选择几个:SPRING JPA

Java 在所有嵌套实体中选择几个:SPRING JPA,java,spring,jpa,spring-data-jpa,Java,Spring,Jpa,Spring Data Jpa,我有一个如下的场景 假设EntityA有三个嵌套实体EntityB、EntityC、EntityD。所有的EntityB,EntityC,EntityD都有几个嵌套的实体 但在选择EntityA时,它会选择嵌套实体的整个树。然而,我想获取一个特定的分支。假设只提取EntityA、EntityB和EntityB的所有子实体,然后离开EntityC和EntityD,我不知道该怎么做。当SpringJPA将所有嵌套对象带回给我时 我正在使用下面的集合映射 @实体 @表(name=“客户方映射”) @

我有一个如下的场景

假设
EntityA
有三个嵌套实体
EntityB、EntityC、EntityD
。所有的
EntityB,EntityC,EntityD
都有几个嵌套的实体

但在选择
EntityA
时,它会选择嵌套实体的整个树。然而,我想获取一个特定的分支。假设只提取
EntityA、EntityB
EntityB
的所有子实体,然后离开
EntityC
EntityD
,我不知道该怎么做。当SpringJPA将所有嵌套对象带回给我时

我正在使用下面的集合映射


@实体
@表(name=“客户方映射”)
@资料
公共级客户满意度{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“id”)
私有整数id;
@列(name=“customer\u id”)
私有整数客户机;
@列(name=“orgtype\u id”)
私有整数orgTypeId;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name=“客户\参与方\映射\ id”)
@Fetch(值=FetchMode.SUBSELECT)
私有列表customerPartyBookingLocation=new ArrayList();
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name=“客户\参与方\映射\ id”)
@Fetch(值=FetchMode.SUBSELECT)
私有列表CustomerPartyFieldMapingEntity=new ArrayList();
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name=“客户\参与方\映射\ id”,referencedColumnName=“id”)
@Fetch(值=FetchMode.SUBSELECT)
私有列表otherDocumentsList=新建
ArrayList();
@OneTONE(cascade={CascadeType.PERSIST,CascadeType.MERGE})
@JoinColumn(name=“customer\u name\u screening\u id”,referencedColumnName=“id”)
私人客户筛选实体客户筛选实体;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name=“客户\参与方\映射\ id”)
@Fetch(值=FetchMode.SUBSELECT)
私有列表customerDocInfoTrackingList=新建
ArrayList();
}

我正在打电话


List CustomerPartyMappingGenetityList=CustomerPartyMappingPropository.FindByCustomerId(customerid);

当我只需要
CustomerPartyMappingEntity
及其
customerPartyFieldMappingEntity
嵌套对象的列表时,它获取所有嵌套映射的实体列表


任何帮助都将不胜感激。

首先使用
FetchType.LAZY
进行嵌套实体。 然后,您可以使用
@EntityGraph
按名称获取嵌套实体,并使用存储库中的
名称获取嵌套实体。您只需在
attributePath
like中指定嵌套属性

@EntityGraph(attributePaths = {"customerPartyBookingLocation"})
@EntityGraph(attributePaths = {"customerPartyFieldMappingEntity.subField"})
以及
customerPartyBookingLocation
like的嵌套属性

@EntityGraph(attributePaths = {"customerPartyBookingLocation"})
@EntityGraph(attributePaths = {"customerPartyFieldMappingEntity.subField"})
例如:

@EntityGraph(attributePaths = {"customerPartyBookingLocation", "customerPartyFieldMappingEntity.subField"})
List<CustomerPartyMappingEntity> findByCustmerId(Integer customerid);
@EntityGraph(attributePath={“customerPartyBookingLocation”,“CustomerPartyFielddMappingEntity.subField”})
列出FindByCustomerId(整数customerid);

注意:如果实体设置正确,则不能将@EntityGraph与@Query annotation一起使用,例如,请参阅子选择示例并删除您的EAGER(您当前正在指示hibernate在实体初始化时获取所有这些字段)。它应该能工作。

好的。然后,如果我想检索嵌套集合的不同组合,那么我必须在存储库中有多个类似的方法?如果是这样的话,我就不能对根实体使用派生查询,而必须使用@query way。。对吗?您不能将
@EntityGraph
@Query
一起使用,您可以将方法命名查询与
@EntityGraph
一起使用。我不确定您所说的“方法命名查询”是什么意思。你是说NamedEntityGraph吗?不,实际上我的意思是“从方法名创建查询”和派生查询之间没有太大区别。它们在下面的用例中都没有用处。假设我仍然想调用
List findbycusterid(Integer customerid)
在五种不同的情况下,在五种不同的情况下,每种情况下都需要一个不同的嵌套列表,而不是一次全部嵌套列表。所以我想我应该使用NamedEntityGraph来实现这一点。无论如何,谢谢你的回复。