1

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);
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

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

Gab
  • 56
  • 6
  • I am using H2 database. do you know how to correctly compose a request? for some reason I thought I could not do without a converter here – Kanari Himawari Dec 05 '21 at 22:12
  • Looks like this page might have the answer. http://hsqldb.org/doc/2.0/guide/builtinfunctions-chapt.html#bfc_extract_datetime Then it would be something like where dayofweek(m.updatedat) = theDayUWant – Gab Dec 06 '21 at 00:55