How to query JPA LocalDateTime field with a LocalDate value?

后端 未结 3 596
名媛妹妹
名媛妹妹 2021-01-21 00:12

I search for a way to get a list of Objects created on a certain LocalDateTime date saved in the Postgresql database in a field of type TIMESTAMPTZ.

To do so, I tried to

3条回答
  •  既然无缘
    2021-01-21 00:45

    Using an answer that was quickly deleted after it was posted (I had to use the column and not the property name, mark the query as nativeQuery and also select with '.*' to avoid org.postgresql.util.PSQLException: The column name id was not found in this ResultSet error), I found this solution:

    @Query(value = "SELECT e.* FROM Entity e WHERE DATE(creation_date) =:date", nativeQuery = true)
    List findByCreationDate(LocalDate date);
    

    Alternatively, following this answer, I tested with success a second solution:

    default List findByCreationDate(LocalDate localDate) {
        return findByPublicationDateBetween(localDate.atStartOfDay(), localDate.plusDays(1).atStartOfDay());
    }
    
    
    List findByCreationDateBetween(LocalDateTime from, LocalDateTime to);
    

提交回复
热议问题