Spring Data JPA findAll与介于两者之间的表(PropertyReferenceException)

时间:2018-10-24 22:50:48

标签: java spring-data-jpa jpql

这是我的模特:

  • 一个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);
}

我该怎么办?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

尝试

  List< AdmisRejet> findAllAdmisRejetByAdmisHistoryWhereAdmisRejecIsNotNul(AdmisHistory admisHistory);

或类似的东西。