0

I have a requirement where User will be able to search for a restaurant based on location and checks the menu of the restaurant. The menu contains various items like soups, starters, main course etc. The admin user should be able to new restaurant, the menu and the items and their prices.

Now in java, I have this relationship, I am adding required code only for clarity:

Restaurant.java

@Entity
public class Restaurant {

    @Id
    private Long id;
    private String name;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "restaurant_menu")
    private Menu menu;
}

Menu.java

@Entity
public class Menu {

    @Id
    private Long id;
    private String name;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "menu_soups", joinColumns = { @JoinColumn(name = "menu_id") }, inverseJoinColumns = { @JoinColumn(name = "soup_id") })
    private Set<Soup> soups = new HashSet<Soup>();
}

Soup.java

@Entity
public class Soup {

    @Id
    private Long id;
    private String name;
}

Now I am confused like where should I keep the price details. Since menu and soup have many-to-many relationship, I should maintain a separate table. But how can I do that in hibernate.

learner
  • 6,062
  • 14
  • 79
  • 139
  • The price is a menu's property or the soup's property? Or both? – Héctor Oct 27 '15 at 14:21
  • Possible duplicate of [Can add extra field(s) to @ManyToMany Hibernate extra table?](http://stackoverflow.com/questions/1153409/can-add-extra-fields-to-manytomany-hibernate-extra-table) – Gimby Oct 27 '15 at 14:41
  • @user3181365 I take no credit, it is the link provided by Dhruv Rai Puri in his link-only answer. This question should be closed as a duplicate, hence I flagged it. – Gimby Oct 27 '15 at 16:07

1 Answers1

0

Check out this - answer which explains how to have attributes on a many-to-many relationship,which is what you need basically.

Community
  • 1
  • 1
Dhruv Rai Puri
  • 1,335
  • 11
  • 20