How to save a List of persistent objects bound to another objects with Hibernate

1.7k views Asked by At

I have a main Object called Menu, with a List of objects VoceMenu

public class Menu implements Serializable{
...
@OneToMany(mappedBy="menu", fetch=FetchType.EAGER)  
private List<VoceMenu> voceMenuList;
...
}

when I edit a object Menu and then I save it with

...
getCurrentSessionFactory().saveOrUpdate(menu);  
...

I can see that on the DB, the value of the fields of the object Menu are edited, the fields of the objects VoceMenu aren't.

Probably I miss something.

1

There are 1 answers

1
mmalik On

Did you set Menu on VoceMenu object before persisting? Try the following:

Menu menu = new Menu();
VoceMenu voce = new VoceMenu();
voce.setMenu(menu);
voceMenuList.add(voce);

...
getCurrentSessionFactory().saveOrUpdate(menu);

Also, you need to add cascade = Cascade.PERSIST to make it happen:

@OneToMany(mappedBy="menu", fetch=FetchType.EAGER, cascade = Cascade.PERSIST)