英文:
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 = "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;
}
My Repository is empty:
public interface CustomerRepository extends JpaRepository<Customer, UUID> {
}
On my service I'm doing something like:
Customer customer = customerRepository.findById(customerId).orElseThrow(() -> new CustomerNotFoundException("customerId", customerId));
But what I would like to do is something like:
if (showRecords) {
Customer customer = customerRepository.findById(customerId).orElseThrow(() -> new CustomerNotFoundException("customerId", customerId));
} else {
Customer customer = customerRepository.findByIdWithoutAssociations(customerId).orElseThrow(() -> new CustomerNotFoundException("customerId", 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<Customer, UUID>{
@EntityGraph(attributePaths = {"records"})
Customer findWithRecordsById(UUID id);
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论