简体   繁体   English

如何使用Hibernate保存绑定到另一个对象的持久对象列表

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

I have a main Object called Menu, with a List of objects VoceMenu 我有一个名为Menu的主对象,其中包含对象列表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. 我可以看到在数据库上,对象Menu的字段的值被编辑,而对象VoceMenu的字段没有被编辑。

Probably I miss something. 可能我想念一些东西。

Did you set Menu on VoceMenu object before persisting? 在持久化之前,是否在VoceMenu对象上设置了Menu? 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: 另外,您需要添加cascade = Cascade.PERSIST来实现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM