开发者

Hibernate/Spring: Persisting collections

开发者 https://www.devze.com 2022-12-22 01:45 出处:网络
I\'ve got two objects: User and Permission, where User has a Collection. In my \"create\" service, I read a bunch of Permissions, put them in a HashSet, add them to the user, and create the user using

I've got two objects: User and Permission, where User has a Collection. In my "create" service, I read a bunch of Permissions, put them in a HashSet, add them to the user, and create the user using my DAO that says

((SessionFactory) sessionFactory).getCurrentSession().save(user);

When I look in my object, all looks fine, but when I look in the database, it hasn't added the开发者_如何学运维 set in the database.

Any suggestions on how I should go about debugging this? Any typical gotchas?

Cheers

Nik


The solution is simply this: Although I added the Permissions HashSet to User, I did not add the user to the Users HashSet in Permission. Hibernate needs both sides to be updated.

Cheers

Nik

PS, to the comments about not enough code, I believe the description was quite enough to make it general enough to be relevant to others who will visit the thread.


Check how you have defined your one-to-many relationship, especially the CASCADE option, and check it has a value that allows to persist collections.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号