gpt4 book ai didi

java - JPA - 按需多对一

转载 作者:行者123 更新时间:2023-12-02 01:55:20 27 4
gpt4 key购买 nike

我有实体工作流,它与 ValidationResults 类具有 @OneToMany 关系。它是延迟获取的,但有时我想获取所有工作流程并对其访问 ValidationResults 进行交互。在那一刻,我希望 jpa 急切地获取所有数据,而不是每次访问 ValidationResults 时都查询。我用的是springDataJpa,怎么做,有什么办法可以用@Query来做吗?

我尝试实现类似的目标,但我不知道如何实现

   //here all the workflows has corresponding data eagerly
List<Workflow> workflows = workflowService.getAllWorkflowsWithValidationResultsEagerly();

//here validationResults ref is lazy, when I try to access it it does query
List<Workflow> workflows = workflowService.getAllWorkflowsUsually();

这是我的实体。

@Entity
@Table(name = "workflow")
public class Workflow {

..............

@OneToMany(fetch = FetchType.LAZY, mappedBy = "workflow", cascade = CascadeType.ALL)
private Set<ValidationResults> validationResultsSet = new HashSet<>();

public Set<ValidationResults> getValidationResultsSet(){return this.validationResultsSet;}
...............
}

和 ValidationResult 类

@Entity
@Table(name = "validation_results")
public class ValidationResults {
...
@ManyToOne
@JoinColumn(name = "workflow_id", insertable = false, updatable = false)
private Workflow workflow;

....
}

最佳答案

执行此操作的 spring boot-ish 方法是使用 @EntityGraph如文档中所述。

关于java - JPA - 按需多对一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52388764/

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