Hi Lester,
giving your example :
Set roles = a_Group.getRoles();
// All groups given the user role. Role role = (Role)m_RolesByName.get(k_RoleWhatever); assert(role != null); roles.add(role); a_Session.save(group);
Whether this succeeds or not depends on the state of a_Group. If I just created the group myself, I would have had to call this code at some point:
Set groupRoles = new HashSet(); a_Group.setRolesAsSet(groupRoles);
I don't think you have to do that, because the getRoles() method always return a RoleSet (empty if you have created the Group yourself).
The methods getRolesAsSet() and setRolesAsSet() should never be used because their purpose is only hibernate mapping,
use getRoles() and setRoles(Set) instead to play with RoleSet objects.
There is another way to attach a group to a role, so that you don't need to care about all this :
get a ModelManager object (an hibernate implementation) and use the "grant(group,role)" method...
this method is doing the job for you (it adds the role in the group's RoleSet and the group in the role's GroupSet).
hope this helps
Lester Ward wrote:
I am not sure exactly what you are trying to achieve and I am sorry if I have got the wrong end of the stick, but when you are adding the security elements you should use the managers.
These encapsulate all the hibernate code for getting them into the database.
The managers seem to involve manipulating single objects. Doesn't this sort of defeat the purpose of using Hibernate, given it's incredibly useful cascade abilities?
Here is what I am doing:
I have a pre-loaded map of roles (which in this system never really change). I have a group and am trying to change it's roles. Based on the API, I'm doing this:
Set roles = a_Group.getRoles();
// All groups given the user role. Role role = (Role)m_RolesByName.get(k_RoleWhatever); assert(role != null); roles.add(role); a_Session.save(group);
Whether this succeeds or not depends on the state of a_Group. If I just created the group myself, I would have had to call this code at some point:
Set groupRoles = new HashSet(); a_Group.setRolesAsSet(groupRoles);
In that case, I get this HashSet back when I call getRolesAsSet and everything works great. If, however, a_Group is an existing group loaded from the database, when I call getRolesAsSet, I get back a Hibernate set that does the proxy thing mentioned in my first mail. Then the roles.add(role) call fails.
The saving of the group automatically saves the relation records with no additional coding, one of the points of using Hibernate. I don't see how the managers help in this case, because I don't see any methods for attaching...
Ah! D'oh! There is my problem! I shouldn't be assigning to an interface! The code should be:
RoleSet roles = a_Group.getRoles();
Interestingly, I got screwed here be a good habit: declaring variables as interfaces, not objects. In other words, I always code like this:
Set mySet = new HashSet(); List myList = new ArrayList(); Collection myCollection = new ArrayList();
...not...
HashSet mySet = new HashSet(); // Bad ArrayList myList = new ArrayList(); // Bad
This habit makes your code much more flexible in the long run, particularly if you need to change performance characteristics. It screws me here because the RoleSet isn't a complete implementation of the Set interface (which makes me stand behind my previous e-mail on how to change it even more).
Wordman
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
