Spring Data JPA / Hibernate 处理关联

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

Spring Data JPA/Hibernate handling associations

问题

根据参数,从实体中检索或不检索一些关联。在下面的示例中,只有在通过我的API传递参数时,我才需要获取记录列表。您能否推荐一种使用Hibernate/Spring Data实现此目标的方法?我正在寻找最清晰且类似于Spring Data的方法。

  1. public class Customer {
  2. private UUID id;
  3. @OneToMany(mappedBy = "customer")
  4. private List<Record> records = new ArrayList<>();
  5. }
  6. public class Record {
  7. private UUID id;
  8. @Column(name = "customer_id", length = 36, columnDefinition = "varchar(36)", nullable = false)
  9. private UUID customerId;
  10. @JoinColumn(name = "customer_id", insertable = false, updatable = false)
  11. private Customer customer;
  12. }

我的Repository为空:

  1. public interface CustomerRepository extends JpaRepository<Customer, UUID> {

}

在我的Service中,我正在做类似这样的事情:

  1. Customer customer = customerRepository.findById(customerId).orElseThrow(() -> new CustomerNotFoundException("customerId", customerId));

但是我想要做类似这样的事情:

  1. if (showRecords) {
  2. Customer customer = customerRepository.findById(customerId).orElseThrow(() -> new CustomerNotFoundException("customerId", customerId));
  3. } else {
  4. Customer customer = customerRepository.findByIdWithoutAssociations(customerId).orElseThrow(() -> new CustomerNotFoundException("customerId", customerId));
  5. }
英文:

I need based on parameter retrieve or not some associations from an entity. In the bellow example I need to get the records list only if a parameter is passed through my api. Can you recommend a way of achieving this using hibernate/spring data? I'm looking for the most clean and spring data-like approach.

  1. public class Customer {
  2. private UUID id;
  3. @OneToMany(mappedBy = &quot;customer&quot;)
  4. private List&lt;Record&gt; records = new ArrayList&lt;&gt;();
  5. }
  6. public class Record {
  7. private UUID id;
  8. @Column(name = &quot;customer_id&quot;, length = 36, columnDefinition = &quot;varchar(36)&quot;, nullable = false)
  9. private UUID customerId;
  10. @JoinColumn(name = &quot;customer_id&quot;, insertable = false, updatable = false)
  11. private Customer customer;
  12. }

My Repository is empty:

  1. public interface CustomerRepository extends JpaRepository&lt;Customer, UUID&gt; {

}

On my service I'm doing something like:

  1. Customer customer = customerRepository.findById(customerId).orElseThrow(() -&gt; new CustomerNotFoundException(&quot;customerId&quot;, customerId));

But what I would like to do is something like:

  1. if (showRecords) {
  2. Customer customer = customerRepository.findById(customerId).orElseThrow(() -&gt; new CustomerNotFoundException(&quot;customerId&quot;, customerId));
  3. } else {
  4. Customer customer = customerRepository.findByIdWithoutAssociations(customerId).orElseThrow(() -&gt; new CustomerNotFoundException(&quot;customerId&quot;, customerId));
  5. }

答案1

得分: 2

关于使用基本的 findById 方法仅返回 Customer 对象,然后使用另一个带有 @EntityGraph 注解的方法 findWithRecordsById 来返回包含 records 的 customer,您觉得如何?

  1. public interface CustomerRepository extends JpaRepository<Customer, UUID> {
  2. @EntityGraph(attributePaths = {"records"})
  3. Customer findWithRecordsById(UUID id);
  4. // ...
  5. }
英文:

How about using the base findById to return just the Customer object and have another method findWithRecordsById to return customer+records using @EntityGraph?

  1. public interface CustomerRepository extends JpaRepository&lt;Customer, UUID&gt;{
  2. @EntityGraph(attributePaths = {&quot;records&quot;})
  3. Customer findWithRecordsById(UUID id);
  4. ...
  5. }

huangapple
  • 本文由 发表于 2020年9月22日 23:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64013319.html
匿名

发表评论

匿名网友

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

确定