
I have a class diagram looking like the picture above. I use NHibernate to do CRUD commands on the objects. When I write to the permission table (adds a role to the user’s role collection and vice versa), NHibernate starts acting strange. I have a Method called UpdateRoles() that makes sure that all Roles are up to date. This method fails as NHibernate sometimes generates proxy objects, hence UpdateRoles() now think the Role does not exist and makes a new object (causing my hierarchy to be duplicated). I found a pattern for when NHibernate loads objects as proxies:

Case 1 works, case 2 doesn’t. What happens is that in case 1 the user is added to the user collection of each Role in each 3 levels of the hierarchy. This is working as intended.
In case 2, the user is only added to the last level in the hierarchy. Now the parent role (uCommerce) is loaded as a proxy object. My RoleMap looks like this:
References(x => x.ParentRole).Nullable().Column("ParentRoleId");
HasManyToMany(x => x.Users).AsSet().Table("uCommerce_Permission");
HasMany(x => x.Roles).Inverse().KeyColumn("ParentRoleId").Cascade.AllDeleteOrphan();
My UserMap looks like this:
HasManyToMany(x => x.Roles).AsSet().Table("uCommerce_Permission");
How can I prevent NHibernate from doing this? I use Fluennt NHibernate. Specifying Not().LazyLoad() doesn't prevent the problem. Also i read that specifying such is a bad case of programming.
UpdateRolesmethod might help - kͩeͣmͮpͥ ͩ