是否可以?
我想使用 JPARepository 获取具有特定 CCC.incidentAssessmentResultId id 的所有 AAA 对象。是否可以?
@Entity
@Table(schema = "aaa", name = "table")
public class AAA {
@Column(name = "kryterium")
private String criterion;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "id_kryterium_naruszen")
private List<BBB> violationFactors;
}
public class BBB {
@Column(name = "czynnik")
private String factor;
@Column(name = "stopien")
private float degree;
@JsonManagedReference
@OneToOne(mappedBy = "violationFactor")
private CCC incidentAssessmentFactor;
}
public class CCC {
@Column(name="komentarz")
private String comment;
@Column(name="ocena_naruszenia_wynik_id", updatable=false, insertable=false)
private Long incidentAssessmentResultId; //-> I'd like to find AAA objects with a specific incidentAssessmentResultId ID
@Column(name="czynnik_wybrany")
private Boolean factorIsSelected;
@Column(name = "wartosc_wybrana")
private float value;
@Repository
public interface ViolationCriterionRepository extends JpaRepository<AAA, Long> {
// @Query("select vc from AAA vc left join vc.violationFactors vf left join vf.incidentAssessmentFactor iaf where iaf.incidentAssessmentResultId = ?1")
List<AAA> findByViolationFactors_IncidentAssessmentFactor_IncidentAssessmentResultId(Long incidentId);
}
现在,当我调用 ViolationCriterionRepository .findAll() 时,我获取了所有数据,但我想获取所有数据,但包含某些 CCC 对象。我已经在我的存储库中尝试了下面的方法,但我得到了 0 个结果。
更新
我的 repo :
@Repository
public interface ViolationCriterionRepository extends JpaRepository<ViolationCriterion, Long> {
@Query("select vc from AAA vc join vc.violationFactors vf join vf.incidentAssessmentFactor iaf where iaf.incidentAssessmentResultId = ?1")
List<AAA> findByIncidentAssessmentResultId(Long incidentId);
}
最佳答案
在 AAA JpaRepository 中:
List<AAA> aaaList = findByViolationFactorsIncidentAssessmentFactorIncidentAssessmentResultId( long incidentAssessmentResultId);
关于Spring Data JPA 通过嵌套对象 id 查找(嵌套两次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56418009/