这是我的模型:


一个AdmisHistory链接到许多Admis
一个Admis链接到0或一个AdmisRejet


实体:

public class AdmisHistory {
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "admisHistory", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Admis> admis = new ArrayList<>();

public class Admis {
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = ADMIS_HISTORY_ID)
    private AdmisHistory admisHistory;

    @OneToOne(mappedBy = "admis", cascade = CascadeType.ALL)
    private AdmisRejet admisRejet;

public class AdmisRejet {
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = ADMIS_ID)
    private Admis admis;


给定一个AdmisHistory,我想检索AdmisRejet的列表。

我设法做到这一点:

public interface AdmisRepository extends CrudRepository<Admis, Long> {
    List<Admis> findAllAdmisByAdmisHistory(AdmisHistory admisHistory);
...

// It work llike this:
 admisRepository.findAllAdmisByAdmisHistory(admisHistory)
.stream()
.filter(adm -> adm.getAdmisRejet() != null)


现在,我想在存储库上的一个简单调用中做到这一点。这样会更加高效和可读。

像这样:

 public interface AdmisRejetRepository extends CrudRepository<AdmisRejet, Long> {
        List< AdmisRejet> findAllAdmisRejetByAdmisHistory(AdmisHistory admisHistory);
    }


但是我得到这个错误:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property history found for type Admis! Traversed path: AdmisRejet.admis.'


我尝试使用@Query,但语法不好:

public interface AdmisRejetRepository extends CrudRepository<AdmisRejet, Long> {
    @Query("SELECT a " +
            "FROM AdmisRejet ar " +
            "LEFT JOIN ar.admis a, " +
            "LEFT JOIN a.admisHistory ah " +
            "WHERE ah = :admisHistory")
    List< AdmisRejet> findAllByHistory(AdmisHistory admisHistory);
}


我该怎么做 ?

最佳答案

如果我阅读的规范正确,那应该可以。

@Query("SELECT DISTINCT ar
        FROM AdmisRejet ar
        WHERE ar.admis.admisHistory = :history")
List<AdmisRejet> findAdmisRejetByAdmisHistory(AdmisHistory history);

07-24 19:58