Spring Data JPA / Hibernate 处理关联

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

Spring Data JPA/Hibernate handling associations

问题

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

public class Customer {
  private UUID id;

  @OneToMany(mappedBy = "customer")  private List<Record> records = new ArrayList<>();
}

public class Record {
    private UUID id;

    @Column(name = "customer_id", length = 36, columnDefinition = "varchar(36)", nullable = false)
    private UUID customerId;
    
    @JoinColumn(name = "customer_id", insertable = false, updatable = false)
    private Customer customer;
}

我的Repository为空:

public interface CustomerRepository extends JpaRepository<Customer, UUID> {

}

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

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

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

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

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.

public class Customer {
  private UUID id;

  @OneToMany(mappedBy = &quot;customer&quot;)

  private List&lt;Record&gt; records = new ArrayList&lt;&gt;();
}

public class Record {
    private UUID id;

    @Column(name = &quot;customer_id&quot;, length = 36, columnDefinition = &quot;varchar(36)&quot;, nullable = false)
    private UUID customerId;
    
    @JoinColumn(name = &quot;customer_id&quot;, insertable = false, updatable = false)
    private Customer customer;
}

My Repository is empty:

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

}

On my service I'm doing something like:

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

But what I would like to do is something like:

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

答案1

得分: 2

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

public interface CustomerRepository extends JpaRepository<Customer, UUID> {

    @EntityGraph(attributePaths = {"records"})
    Customer findWithRecordsById(UUID id);
    // ...
}
英文:

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

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

    @EntityGraph(attributePaths = {&quot;records&quot;})
    Customer findWithRecordsById(UUID id);
...
}

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:

确定