views:

65

answers:

0

I have a Hibernate entity named Menu which has a collection of Groups, each group in turn has a collection of MenuItems. So as an example, a menu can be for a restaurant, groups can be Lunch and Dinner and the menuItems within these can be Pasta, Burger, Salad.

The problem I'm having is that once i have created the menu and saved it (which works fine), when i try to get the menu back I am getting more groups than were originally created. So taking the example above, if i put Burger and Salad in Dinner group and Pasta in Lunch, I am returned a Menu with THREE (rather than TWO) groups: 2 Dinner groups (each with the items i put in) and 1 Lunch group. I basically get back as mayny groups as the number of menu items i inserted into each group. So if i had inserted 4 items, 4 groups are returned. Anyone know why this may be?

The relationships are: Menu to Group = One-to-Many, Group to MenuItems = Many-to-Many

Groups are not reused. They will be unique to one menu. But MenuItems can be reused in many groups.

Here is my code:

class Menu {
   @Id @GeneratedValue(strategy = GenerationType.AUTO)
   private long menuID;

   @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)     
   @JoinColumn(name="menuID")
   private List<MenuGroup> groups;
}

class MenuGroup {
   @Id @GeneratedValue(strategy = GenerationType.AUTO)
   private long groupID;

   @Sort (type=SortType.NATURAL)
   @ManyToMany(fetch=FetchType.EAGER)
   @JoinTable(name="group_menu_item", 
              joinColumns = { @JoinColumn(name = "groupID") }, 
              inverseJoinColumns = { @JoinColumn(name = "menuItemID") } )
   private SortedSet<MenuItem> menuItems;
}

class MenuItem {
   @Id @GeneratedValue(strategy = GenerationType.AUTO)
   private long menuItemID;
}