Java JPA-QL查询查找LocalDateTime时间戳在LocalDate startDate和LocalDate endDate之间的所有实体

Java JPA-QL查询查找LocalDateTime时间戳在LocalDate startDate和LocalDate endDate之间的所有实体,java,hibernate,jpa,jsr310,Java,Hibernate,Jpa,Jsr310,我有一个JPA实体时隙,其中有一个名为startDateTime的LocalDateTime字段: @Entity public class TimeSlot { private LocalDateTime startDateTime; ... } 我正在WildFly 10.1上使用Hibernate。如何查询startDateTime介于startDate和endDate之间的所有实体 private List<TimeSlot> getTimeSlotsBy

我有一个JPA实体时隙,其中有一个名为
startDateTime
LocalDateTime
字段:

@Entity
public class TimeSlot {

    private LocalDateTime startDateTime;
    ...
}
我正在WildFly 10.1上使用Hibernate。如何查询
startDateTime
介于
startDate
endDate
之间的所有实体

private List<TimeSlot> getTimeSlotsByStartDateEndDate(LocalDate startDate, LocalDate endDate) {
    return entityManager.createNamedQuery("TimeSlot.findByStartDateEndDate", TimeSlot.class)
            .setParameter("startDate", startDate)
            .setParameter("endDate", endDate).getResultList());
}

必须将LocalDateTime和LocalDate转换为java.sql.Timestamp,然后将转换器类添加到persistent.xml文件中,然后一切都必须正常。 对于LocalDateTimeConverter:

import java.time.LocalDateTime;
import java.sql.Timestamp;
 
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
     
    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
        return locDateTime == null ? null : Timestamp.valueOf(locDateTime);
    }
 
    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
        return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime();
    }
}
import java.time.LocalDateTime;
导入java.sql.Timestamp;
@转换器(自动应用=真)
公共类LocalDateTimeAttributeConverter实现AttributeConverter{
@凌驾
公共时间戳convertToDatabaseColumn(LocalDateTime locDateTime){
返回locDateTime==null?null:Timestamp.valueOf(locDateTime);
}
@凌驾
公共LocalDateTime convertToEntityAttribute(时间戳sqlTimestamp){
返回sqlTimestamp==null?null:sqlTimestamp.toLocalDateTime();
}
}
对于LocalDateTime:

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();
    }
}
import java.sql.Date;
导入java.time.LocalDate;
@转换器(自动应用=真)
公共类LocalDateAttributeConverter实现AttributeConverter{
@凌驾
public Date convertToDatabaseColumn(LocalDate locDate){
return locDate==null?null:Date.valueOf(locDate);
}
@凌驾
公共LocalDate convertToEntityAttribute(日期sqlDate){
返回sqlDate==null?null:sqlDate.toLocalDate();
}
}
最后,将类添加到persistent.xml

<class>xxxx.model.Entities</class>
<class>xxxx.converter.LocalDateConverter</class>
<class>xxxx.converter.LocalDateTimeConverter</class>
xxxx.model.Entities
xxxx.converter.LocalDateConverter
xxxx.converter.LocalDateTimeConverter

必须将LocalDateTime和LocalDate转换为java.sql.Timestamp,然后将转换器类添加到persistent.xml文件中,然后一切都必须正常。 对于LocalDateTimeConverter:

import java.time.LocalDateTime;
import java.sql.Timestamp;
 
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
     
    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
        return locDateTime == null ? null : Timestamp.valueOf(locDateTime);
    }
 
    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
        return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime();
    }
}
import java.time.LocalDateTime;
导入java.sql.Timestamp;
@转换器(自动应用=真)
公共类LocalDateTimeAttributeConverter实现AttributeConverter{
@凌驾
公共时间戳convertToDatabaseColumn(LocalDateTime locDateTime){
返回locDateTime==null?null:Timestamp.valueOf(locDateTime);
}
@凌驾
公共LocalDateTime convertToEntityAttribute(时间戳sqlTimestamp){
返回sqlTimestamp==null?null:sqlTimestamp.toLocalDateTime();
}
}
对于LocalDateTime:

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();
    }
}
import java.sql.Date;
导入java.time.LocalDate;
@转换器(自动应用=真)
公共类LocalDateAttributeConverter实现AttributeConverter{
@凌驾
public Date convertToDatabaseColumn(LocalDate locDate){
return locDate==null?null:Date.valueOf(locDate);
}
@凌驾
公共LocalDate convertToEntityAttribute(日期sqlDate){
返回sqlDate==null?null:sqlDate.toLocalDate();
}
}
最后,将类添加到persistent.xml

<class>xxxx.model.Entities</class>
<class>xxxx.converter.LocalDateConverter</class>
<class>xxxx.converter.LocalDateTimeConverter</class>
xxxx.model.Entities
xxxx.converter.LocalDateConverter
xxxx.converter.LocalDateTimeConverter

BETWEEN操作符中的
似乎至少有些数据库不喜欢混合日期和日期时间/时间戳。您正在传递
LocalDate
s(
startDate
endDate
),它显然会强制转换为日期,而
t.startDateTime
是一个时间戳。我可以想出两种解决方案(但目前无法验证):(1)在Java中将
LocalDate
转换为
LocalDateTime
,或者(2)使用JPA的
函数
并将日期转换为数据库中的时间戳,为基础数据库使用特定于数据库的函数。我会同意。好运:)1)意味着知道一天的第一个和最后一个时间戳是什么。闰秒让这变得不那么有趣。我将首先调查2)。调用的SQL是什么?似乎至少有些数据库不喜欢在
BETWEEN
操作符中混合日期和日期时间/时间戳。您正在传递
LocalDate
s(
startDate
endDate
),它显然会强制转换为日期,而
t.startDateTime
是一个时间戳。我可以想出两种解决方案(但目前无法验证):(1)在Java中将
LocalDate
转换为
LocalDateTime
,或者(2)使用JPA的
函数
并将日期转换为数据库中的时间戳,为基础数据库使用特定于数据库的函数。我会同意。好运:)1)意味着知道一天的第一个和最后一个时间戳是什么。闰秒让这变得不那么有趣。我先调查一下。调用的SQL是什么?