gpt4 book ai didi

java - Spring Data JPA findAll 之间有表(PropertyReferenceException)

转载 作者:行者123 更新时间:2023-12-02 10:40:03 26 4
gpt4 key购买 nike

这是我的模型:

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

关于java - Spring Data JPA findAll 之间有表(PropertyReferenceException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52978966/

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