[英]JPA select entities with date below parameter
I'm trying to select all entities where the value of the date field is below a given argument. 我试图选择日期字段的值低于给定参数的所有实体。 However, my code doesn't seem to work. 但是,我的代码似乎不起作用。
SomeEntity.java SomeEntity.java
@Entity
public class SomeEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private ZonedDateTime date = ZonedDateTime.now(ZoneId.of("UTC"));
}
SomeEntityRepository.java SomeEntityRepository.java
public interface SomeEntityRepositoryextends CrudRepository<SomeEntity, Long> {
/**
* Get all SomeEntity created before :until
*/
@Query("Select e from SomeEntity e where e.date < :until")
List<SomeEntity> findAllUntil(@Param("until") ZonedDateTime until);
}
My problem is this: 我的问题是这样的:
I will have a SomeEntity
object where the date is 2019-08-31T10:00:00.000+02:00
and the value of until
is 2019-08-24T10:00:00.000+02:00
(seven days earlier) 我将有一个SomeEntity
对象,其日期为2019-08-31T10:00:00.000+02:00
, until
的值是2019-08-24T10:00:00.000+02:00
(七天前)
However, the object is still returned from the query. 但是,该对象仍从查询中返回。
I am using PostgreSQL 9.6 我正在使用PostgreSQL 9.6
Not every JDBC driver supports it. 并非每个JDBC驱动程序都支持它。 You can use one of following solutions: 您可以使用以下解决方案之一:
Use TimeStamp instead of ZonedDateTime. 使用时间戳而不是ZonedDateTime。
Use proper driver and follow its configuration requirements. 使用正确的驱动程序并遵循其配置要求。 For instance, for Oracle you may need to specify converter: 例如,对于Oracle,您可能需要指定转换器:
@Convert(converter=OracleZonedDateTimeSeriliazer.class)
private ZonedDateTime date = ZonedDateTime.now(ZoneId.of("UTC"));
For other databases you may need to implement such a convertor and specify it via @Convert
. 对于其他数据库,您可能需要实现这样的转换器并通过@Convert
指定它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.