How to configure converter from LocalDateTime to LocalDate?

Viewed 78

I have a table of restaurants and their menus. The creation and update of the dish is saved in LocalDateTime. I would like to search for the menu of the day, being able to enter a date without time and take the current date by default. The problem arises because I am storing dishes with LocalDateTime so that the exact time can be viewed. The current code works if I specify LocalDateTime, but I would like LocalDate. I would be grateful for ideas ^_^

@Entity
@Table(name = "dish")
@Setter
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Dish extends BaseEntity {
    public Dish(Integer id, String title, LocalDateTime createdAt, LocalDateTime updatedAt, Integer price) {
        super(id);
        this.title = title;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
        this.price = price;
    }

    @Column(name = "title", nullable = false)
    @NotEmpty
    @Size(max = 128)
    private String title;

    @CreationTimestamp
    @Column(name = "created_at", nullable = false, insertable = false, updatable = false)
    @NotNull
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false, insertable = false, updatable = false)
    @NotNull
    private LocalDateTime updatedAt;

    @Column(name = "price", nullable = false)
    @Range(min = 10, max = 100000)
    private Integer price;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "restaurant_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonBackReference
    private Restaurant restaurant;
}

repository

@Query("SELECT DISTINCT r from Restaurant r JOIN FETCH r.menu m where m.updatedAt = ?1")
    List<Restaurant> findAllByMenuByLocalDateTime(LocalDateTime date);

controller

@GetMapping("/all-menu-of-the-day")
    public List<Restaurant> findAllByMenuByLocalDateTime(
            @RequestParam(defaultValue = "#{T(java.time.LocalDateTime).now()}")
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date) {
        return restaurantService.findAllByMenuByLocalDateTime(date);
    }
1 Answers

Have you tried to only get the date from your m.updatedAt in the query? Not sure if you are in SQL or SQL Server

Related