我正在尝试将以下 SQL 查询转换为 JPQL,以便将其适合 Spring Data @Query。我找不到任何提及此特定案例的文档。有没有人尝试过,并且成功过?我想避免诉诸本机查询。
select distinct gar.id_gar, toplink.start_date as start_date_link, toplink.toplink, ptxref.id_prde_taux_ref, ptxref.end_date_prde
from gar,
lateral
(select link.id_link_gar_h_taux_ref as id_toplink, link.start_date_effet_link as start_date, link.id_taux_ref_h, link.end_date_effet_link, link.in_annulation from link
where link.id_gar = gar.id_gar
order by link.start_date_effet_link asc
limit 1) as toplink
join txref on txref.id_taux_ref_h = toplink.id_taux_ref_h
join ptxref on ptxref.id_taux_ref_h = txref.id_taux_ref_h
where gar.id_gar in (52,53,50)
and (toplink.end-date_effet_link is null or toplink.end-date_effet_link>= '2000-01-01')
and toplink.in_annulation is false
and (ptxref.end_date_prde is null or ptxref.end_date_prde >= '2000-01-01')
我尝试将 SQL 查询转换为 JPQL 查询,但没有任何效果,而且我找不到任何文档。
请按照以下步骤操作
- 首先确定您选择的字段是什么
- 考虑您选择的字段编写界面,例如 InterfaceGarDto
public interface InterfaceGarDto {
Long getIdGar();
LocalDate getStartDateLink();
String getToplink();
String getIdPrdeTauxRef();
LocalDate getEndDatePrde();
}
- 考虑您的实体/域编写 JPQL 查询
- 使用 EntityManager 从下面的代码中获取您期望的结果
Query q = entityManager.createQuery("[ your jpql query ]");
List<InterfaceGarDto> resultList = q.getResultList();