views:

107

answers:

2

Hi guys,

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

A: 

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.

pedromarce
This set is using a MappedBy="users", targetEntity=Permission.class, and Permission.class uses cascade={ CascadeType.PERSIST, CascadeType.MERGE }. Does that look ok?
niklassaers
You should define the cascade option where you define your one-to-many relationship in the User.class.
pedromarce
Ah, it's a many-to-many relationship. Many users can have many permissions
niklassaers
You still have to define the cascade option in the many-to-many relationship in the User.class.
pedromarce
+1  A: 

Hi guys,

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.

niklassaers
no, it wasn't. You see that 3 answers were given that could be the problem, but were ruled-out after your clarifications.
Bozho
+1 to Bozho's comments. @niklassaers - If you had provided relevant information in the first place you would not have wasted time and efforts of other people. And to top it you behave as if you did nothing wrong.
Padmarag
Please, tell me how readers could magically understand that the association was bidirectional? If you're not able to express the problem correctly, have at least the decency to admit it and to provide relevant details to people trying to help when they are asking for instead of making ridiculous assumptions. And you really need to learn how to ask questions the smart way (http://catb.org/~esr/faqs/smart-questions.html).
Pascal Thivent
Hi Pascal, now that you're saying it, I guess they couldn't from the text.
niklassaers