gpt4 book ai didi

java - jpa查询两个日期之间

转载 作者:行者123 更新时间:2023-12-01 19:08:36 26 4
gpt4 key购买 nike

这是我在 SQL Server 上的查询:

select count(*) from noleggi 
where id_utente = 1 AND id_libro =25 and GETDATE() BETWEEN inizio_prestito AND fine_prestito

使用 JPA 存储库的正确语法是什么?

我尝试过这个:

@Query(value = "SELECT COUNT(n) from Noleggio n " +
"WHERE n.libroId = ?1 AND n.utenteId=?2 AND CURRENT_DATE() BETWEEN n.inizioPrestito AND n.finePrestito")

Long countByUtenteIdAndLibroId(Long idLibro, Long idUtente, LocalDate inizioPrestito, LocalDate finePrestito);

并收到此错误:

Validation failed for query for method public abstract java.lang.Long com.finance.biblioteca.repository.NoleggioRepository.countByUtenteIdAndLibroId(java.lang.Long,java.lang.Long,java.time.LocalDate,java.time.LocalDate

最佳答案

您需要实现AttributeConverter<LocalDate, Date>及其两个方法的接口(interface) convertToDatabaseColumnconvertToEntityAttribute

import java.sql.Date;
import java.time.LocalDate;

@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {

@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return locDate == null ? null : Date.valueOf(locDate);
}

@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return sqlDate == null ? null : sqlDate.toLocalDate();
}
}

请参阅此链接了解更多详细信息。 https://thoughts-on-java.org/persist-localdate-localdatetime-jpa/

关于java - jpa查询两个日期之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503543/

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