How to create hibernate request with localdate

Tags: , , ,



I have a user who has an expiration date (dateTime), I need to display all users who have not expired yet. Tried different options with DATA (), or as an example –

@Query (name = "SELECT u FROM User u WHERE u.date>: now")
     Page <User> findByMute (@Param ("now") LocalDateTime now, Pageable pageable);

but it doesn’t output anything. But when asked in MySQL – SELECT * FROM user where user.date> NOW () displays everything as it should. Tell me how to implement this query in Hibernate?

Answer

Replace annotation attribute “name” with “value“:

@Query(value = "SELECT u FROM User u WHERE u.date>:now")
Page<UserEntity> findByMute(@Param("now") LocalDateTime now, Pageable pageable);

The annotation attribute “name” always refers to a named query, while the attribute “value” can be used to specify a query. “value=” can be omitted, so this works too:

@Query("SELECT u FROM UserEntity u WHERE u.date>:now")
Page<UserEntity> findByMute(@Param("now") LocalDateTime now, Pageable pageable);

See https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query for details.



Source: stackoverflow