简体   繁体   中英

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

@Entity
public class SomeEntity {
  @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private ZonedDateTime date = ZonedDateTime.now(ZoneId.of("UTC"));
}

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)

However, the object is still returned from the query.

I am using PostgreSQL 9.6

Not every JDBC driver supports it. You can use one of following solutions:

  1. Use TimeStamp instead of ZonedDateTime.

  2. Use proper driver and follow its configuration requirements. For instance, for Oracle you may need to specify converter:

    @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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM