gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:13:12 25 4
gpt4 key购买 nike

我有一个像下面这样的场景。
让我们说 EntityA具有三个嵌套实体 EntityB, EntityC, EntityD .以及所有 EntityB, EntityC, EntityD里面有几个嵌套的实体。
但是在选择EntityA时它选择嵌套实体的整个树。而我想获取特定的分支。只能说 EntityA, EntityB以及 EntityB 的所有子实体被取走EntityCEntityD那时我不知道该怎么做。随着 spring jpa 将所有嵌套对象带回给我。
我正在使用以下集合映射。

 @Entity
@Table(name = "customer_party_mapping")
@Data
public class CustomerPartyMappingEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;

@Column(name = "customer_id")
private Integer custmerId;

@Column(name = "orgtype_id")
private Integer orgTypeId;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
@JoinColumn(name = "customer_party_mapping_id")
@Fetch(value = FetchMode.SUBSELECT)
private List<CustomerPartyBookingLocationEntity> customerPartyBookingLocation=new ArrayList<CustomerPartyBookingLocationEntity>();

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
@JoinColumn(name = "customer_party_mapping_id")
@Fetch(value = FetchMode.SUBSELECT)
private List<CustomerPartyFieldMappingEntity> customerPartyFieldMappingEntity=new ArrayList<CustomerPartyFieldMappingEntity>();

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
@JoinColumn(name = "customer_party_mapping_id",referencedColumnName="id")
@Fetch(value = FetchMode.SUBSELECT)
private List<CustomerPartyOtherDocumentEntity> otherDocumentsList=new
ArrayList<>();

@OneToOne( cascade={ CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "customer_name_screening_id", referencedColumnName="id")
private CustomerNameScreeningEntity customerNameScreeningEntity;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
@JoinColumn(name = "customer_party_mapping_id")
@Fetch(value = FetchMode.SUBSELECT)
private List<CustomerDocInfoTrackingEntity> customerDocInfoTrackingList=new
ArrayList<CustomerDocInfoTrackingEntity>();
}

我在打电话
List<CustomerPartyMappingEntity> customerPartyMappingEntityList = customerPartyMappingRepository.findByCustmerId(customerid);

它获取所有嵌套的实体映射列表,而我只需要 CustomerPartyMappingEntity及其列表 customerPartyFieldMappingEntity嵌套对象。
任何帮助将不胜感激。

最佳答案

第一次使用 FetchType.LAZY对于嵌套实体。
那么你可以使用@EntityGraph使用 . 按名称获取嵌套实体,并使用其名称获取嵌套实体在存储库中。您过去只在 attributePaths 中指定嵌套属性喜欢

@EntityGraph(attributePaths = {"customerPartyBookingLocation"})
以及 customerPartyBookingLocation 的嵌套属性喜欢
@EntityGraph(attributePaths = {"customerPartyFieldMappingEntity.subField"})
例子:
@EntityGraph(attributePaths = {"customerPartyBookingLocation", "customerPartyFieldMappingEntity.subField"})
List<CustomerPartyMappingEntity> findByCustmerId(Integer customerid);
注意:您不能将 @EntityGraph 与 @Query 注释一起使用

关于java - 在所有嵌套实体中选择几个 : SPRING JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63213174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com