我有实体工作流,它与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 Bootish方式是通过使用@EntityGraph,如文档中所述。

09-05 19:59