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

huangapple go评论109阅读模式
英文:

Select few among all the nested entities : SPRING JPA

问题

I understand that you want to retrieve only the CustomerPartyMappingEntity and its list of customerPartyFieldMappingEntity nested objects from your code. You can achieve this by modifying your query. Here's a suggestion:

List<CustomerPartyMappingEntity> customerPartyMappingEntityList = customerPartyMappingRepository.findCustomerPartyMappingWithFields(customerid);

Then, in your repository interface, define a custom query method like this:

public interface CustomerPartyMappingRepository extends JpaRepository<CustomerPartyMappingEntity, Integer> {
    @Query("SELECT DISTINCT cpm FROM CustomerPartyMappingEntity cpm LEFT JOIN FETCH cpm.customerPartyFieldMappingEntity WHERE cpm.custmerId = :customerId")
    List<CustomerPartyMappingEntity> findCustomerPartyMappingWithFields(@Param("customerId") Integer customerId);
}

This custom query uses a LEFT JOIN FETCH clause to load only the CustomerPartyMappingEntity and its list of customerPartyFieldMappingEntity nested objects, excluding the other nested entities.

Make sure to adjust the query method and entity names as per your actual code structure.

英文:

I have a scenario like below.

Lets say EntityA has three nested entities EntityB, EntityC, EntityD. And all of EntityB, EntityC, EntityD has several nested entities inside them.

But while selecting for EntityA it selects the whole tree of nested entities. Whereas I want to fetch a specific branch. Lets say only EntityA, EntityB and all sub entities of EntityB are to be fetched leaving EntityC and EntityD back then I am not sure how to do that. As spring jpa brings all the nested objects back to me.

I am using below collection mapping.


 @Entity
 @Table(name = &quot;customer_party_mapping&quot;)
 @Data
 public class CustomerPartyMappingEntity {
	
    @Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = &quot;id&quot;)
	private Integer id;
	
	@Column(name = &quot;customer_id&quot;)
	private Integer custmerId;
	
	@Column(name = &quot;orgtype_id&quot;)
	private Integer orgTypeId;
	
	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
	@JoinColumn(name = &quot;customer_party_mapping_id&quot;)
	@Fetch(value = FetchMode.SUBSELECT)
	private List&lt;CustomerPartyBookingLocationEntity&gt; customerPartyBookingLocation=new ArrayList&lt;CustomerPartyBookingLocationEntity&gt;();
	
	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
	@JoinColumn(name = &quot;customer_party_mapping_id&quot;)
	@Fetch(value = FetchMode.SUBSELECT)
	private List&lt;CustomerPartyFieldMappingEntity&gt; customerPartyFieldMappingEntity=new ArrayList&lt;CustomerPartyFieldMappingEntity&gt;();

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
	@JoinColumn(name = &quot;customer_party_mapping_id&quot;,referencedColumnName=&quot;id&quot;)
	@Fetch(value = FetchMode.SUBSELECT)
	private List&lt;CustomerPartyOtherDocumentEntity&gt; otherDocumentsList=new 
    ArrayList&lt;&gt;();
	
	@OneToOne( cascade={ CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = &quot;customer_name_screening_id&quot;, referencedColumnName=&quot;id&quot;) 
	private CustomerNameScreeningEntity customerNameScreeningEntity;
		
	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
	@JoinColumn(name = &quot;customer_party_mapping_id&quot;)
	@Fetch(value = FetchMode.SUBSELECT)
	private List&lt;CustomerDocInfoTrackingEntity&gt; customerDocInfoTrackingList=new 
    ArrayList&lt;CustomerDocInfoTrackingEntity&gt;();
}

And I am calling


List&lt;CustomerPartyMappingEntity&gt; customerPartyMappingEntityList = customerPartyMappingRepository.findByCustmerId(customerid);

It gets all the nested mapped list of entities wheras I need only CustomerPartyMappingEntity and its list of customerPartyFieldMappingEntity nested object.

Any help will be appreciated.

答案1

得分: 11

首先,对于嵌套实体,可以使用 FetchType.LAZY

然后,您可以使用 @EntityGraph 来按名称获取嵌套实体,以及使用它们的名称和 . 在存储库中获取它们的嵌套实体。您只需在 attributePaths 中指定嵌套属性,如下所示:

@EntityGraph(attributePaths = {"customerPartyBookingLocation"})

以及 customerPartyBookingLocation 的嵌套属性,如下所示:

@EntityGraph(attributePaths = {"customerPartyFieldMappingEntity.subField"})

示例:

@EntityGraph(attributePaths = {"customerPartyBookingLocation", "customerPartyFieldMappingEntity.subField"})
List<CustomerPartyMappingEntity> findByCustomerId(Integer customerId);

注意: 无法在 @Query 注解中使用 @EntityGraph

英文:

First use FetchType.LAZY for nested entity.
Then you can use @EntityGraph to fetch nested entity by name and their nested entity using their name with . in the repository. You use to just specify the nested property in attributePaths like

@EntityGraph(attributePaths = {&quot;customerPartyBookingLocation&quot;})

And the nested property of customerPartyBookingLocation like

@EntityGraph(attributePaths = {&quot;customerPartyFieldMappingEntity.subField&quot;})

Example:

@EntityGraph(attributePaths = {&quot;customerPartyBookingLocation&quot;, &quot;customerPartyFieldMappingEntity.subField&quot;})
List&lt;CustomerPartyMappingEntity&gt; findByCustmerId(Integer customerid);

Note: You can't use @EntityGraph with @Query annotation

答案2

得分: 0

如果你的实体确实已正确设置,例如请查看子选择示例此处,并移除EAGER(目前您正在指示Hibernate在实体初始化时获取所有这些字段)。它应该会起作用。

英文:

If your entities are really setup correctly, see for instance the subselect example here and remove your EAGER (you are currently instructing hibernate to fetch all these fields upon entity initialization). It should work.

huangapple
  • 本文由 发表于 2020年8月2日 14:44:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63213174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定