How to query LocalDateTime with LocalDate?

后端 未结 5 1239
一向
一向 2021-01-22 02:49

I\'ve got a class which contains an atttribute of java.time.LocalDateTime type.

public class MyClass{
    // ...
    private LocalDateTime fecha;
    // ...
}
         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-22 02:51

    I had a similar question today (I was making SELECT, not a DELETE) and after posting it on here, I came up with two solutions:

    @Query(value = "DELETE FROM MyClass mc WHERE DATE(fecha) =:fecha", nativeQuery = true)
    public void deleteByFecha(LocalDate fecha);
    

    Alternatively, following Cepr0's answer, I tested with success his solution:

    default void deleteByFecha(LocalDate fecha) {
        deleteByFechaBetween(fecha.atStartOfDay(), fecha.plusDays(1).atStartOfDay());
    
    }
    
    void deleteByFechaBetween(LocalDateTime from, LocalDateTime to);
    

提交回复
热议问题