I'm flat out stuck. I've tried to accomplish this several different ways and I've yet to achieve the desired results. I've read through the forums, FAQs, all applicable chapters in the documentation, and googled on it. Alas, I'm just stuck.
I simply want to map a many-to-many relationship that cascades. For example, map the tables users, users_roles, and roles together. From the code I'd like to perform something similar to:
Set roles = new HashSet();
Set users = new HashSet();
User user = new User();
roles.add(new Role(properties..., users));
user.setRoles(roles);
session.save(roles);
or even this would work:
User user = new User();
Role role = new Role();
Set userRoles = new HashSet();
UserRole userRole = new UserRole(user, role);
userRoles.add(userRole);
user.setUserRoles(userRole);
role.setUserRoles(userRole);
session.save(user);
session.save(role)
and have Hibernate writeout the user, role, and the association. I've attempted using a One-To-Many, with cascade="all", inverse="true" set, to user_roles and have gotten Hibernate to save the User and Role, but won't cascade and save the association. I've attempted using a Many-To-Many with cascade="all", inverse="true" set, mapping User to Roles and Roles to Users, but this generated a "SQL insert, update or delete failed (row not found)" exception on the .save() call. Hibernate appears to be trying to update the Role instead of inserting. I've read the FAQ and understand that I should directly call save on the object, but they defeats the purpose of the cascade.
However, in a vain attempt to get this working I did call save on both the User and Role objects and both were inserted into the database. Unfortunatly, the association wasn't. Any help/suggestions/links would be wonderful.
Thank you in advance for your time and help,
Tyler Pitchford
|