0

I have Order entity, each order has a list of Dish entity.

Only important part showed for brevity.

    @Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Table(name = "orders")
@ToString
public class Order {
    @OnDelete(action = OnDeleteAction.CASCADE)
    @OneToMany(
            mappedBy = "order",
            cascade = CascadeType.ALL,
            orphanRemoval = true)
    @ToString.Exclude
    private List<Dish> dishes;
}

My front sends me a Hashmap of dish.id and quantity of this dish ordered.

To convert it to Order entity I have to add the same dish to list times it was ordered. For example. if order has 5 dishes of id 1, I fetch id 1 and add it 5 times to list, then - save order.

To display this again in front - I use Collections.frequency to convert the Order back to Hashmap.

Should I convert my relationship container to hashmap? I've heard that I should avoid using hashmaps in Hibernate.

  • 1
    I’ve already answered to the pretty same question, have a look, please https://stackoverflow.com/a/70184988/5020294 – Andriy Slobodyanyk Feb 05 '22 at 21:16
  • 1
    Also, it worths to pay your attention that Dish equals() and hashCode should be implemented in the correct way https://vladmihalcea.com/the-best-way-to-implement-equals-hashcode-and-tostring-with-jpa-and-hibernate/ – Andriy Slobodyanyk Feb 05 '22 at 21:19

0 Answers0