Hi Carmine,

I think the problem is to do with how you are saving your entities. I took
your mappings and entities, and rewrote your saving code as follows:

 using (var session = _sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                Role r;
                Privilege p = new Privilege();
                p.Path = "*.a";
                foreach (DefaultRoleNames name in Enum.GetValues(typeof
     (DefaultRoleNames)))
                {
                    r = new Role();
                    r.Name = name;
                    r.AddPrivilege(p); ;
                    session.Save(r); <- direct save here, no repository call
                }

                tx.Commit();
            }

This works:

NHibernate: INSERT INTO roles (Name) VALUES (@p0); select
last_insert_rowid();@p0 = 'Role1'
NHibernate: INSERT INTO privileges (Path) VALUES (@p0); select
last_insert_rowid();@p0 = '*.a'
NHibernate: INSERT INTO roles (Name) VALUES (@p0); select
last_insert_rowid();@p0 = 'Role2'
NHibernate: INSERT INTO roles (Name) VALUES (@p0); select
last_insert_rowid();@p0 = 'Role3'
NHibernate: INSERT INTO RolePrivilege (Role_id, Privilege_id) VALUES
(@p0, @p1);@p0 = 1, @p1 = 1
NHibernate: INSERT INTO RolePrivilege (Role_id, Privilege_id) VALUES
(@p0, @p1);@p0 = 2, @p1 = 1
NHibernate: INSERT INTO RolePrivilege (Role_id, Privilege_id) VALUES
(@p0, @p1);@p0 = 3, @p1 = 1

Since you have cascading on in both directions, saving the privilege instead
of all the roles also works:

 using (var session = _sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                Role r;
                Privilege p = new Privilege();
                p.Path = "*.a";
                foreach (DefaultRoleNames name in Enum.GetValues(typeof
     (DefaultRoleNames)))
                {
                    r = new Role();
                    r.Name = name;
                    r.AddPrivilege(p); ;
                }
                session.Save(p); <-save the privilege here instead

                tx.Commit();
            }

(very similar sql is generated)

I think the combination of your mappings and domain model is a little
confusing, because you've made role the "owner" of the association in that
it has an AddPrivilege method, and yet in your mappings you map the
association from the privilege side and call Inverse(), which makes
privilege the "owner". However while this can make things confusing, I don't
think its the cause of your problem - I suspect your repository
implementation is the problem, as replacing the call to the repository with
a session.save worked fine for me.

On Tue, Oct 6, 2009 at 9:15 PM, Paul Batum <[email protected]> wrote:

> Hudson, I don't think either of those suggestions will fix it. He
> definitely needs those initializers, otherwise:
> var r = new Role();
> r.AddPrivledge(new Privledge());
> will throw an exception.
>
> I'm also fairly sure that those private setters are fine. I'm just now
> loading the code up to see if I can find out what the problem is.
>
>
> On Tue, Oct 6, 2009 at 3:15 AM, Hudson Akridge 
> <[email protected]>wrote:
>
>> Erm, for 1.) I meant the Setter. If it's private, NH will not be able to
>> set it to the values needed when you hydrate the collection.
>>
>>
>> On Mon, Oct 5, 2009 at 11:14 AM, Hudson Akridge <[email protected]
>> > wrote:
>>
>>> Hrm, as for mapping, it seems like your mapping looks correct. However,
>>> were a couple things I noticed:
>>>
>>> 1.) Change the scope of the RolesWithThisPrivilege Collection property
>>> to be protected internal 2.) Remove the collection initializers from the
>>> default constructors on both classes. You need a default constructor for
>>> NHibernate, but it should remain empty
>>>
>>>
>>> On Mon, Oct 5, 2009 at 11:00 AM, CarmineM <[email protected]>wrote:
>>>
>>>>
>>>> Hi to everyone,
>>>>
>>>> First and foremost, I beg your pardon if the question is a trivial
>>>> one, but I'm struggling with it since a few hours. I'm a total newbie
>>>> at both NHibernate and FluentNHibernate.
>>>>
>>>> I have a Domain model with the following classes:
>>>>
>>>> - Role
>>>> - Privilege
>>>>
>>>> Role and privileges are tied together with a HasManyToMany
>>>> relationship.
>>>>
>>>>    public class Role : IAggregateRoot
>>>>    {
>>>>        public virtual int Id { get; private set; }
>>>>        public virtual DefaultRoleNames Name { get; set; }
>>>>        public virtual IList<Privilege> Privileges { get; set; }
>>>>
>>>>        public Role()
>>>>        {
>>>>            Privileges = new List<Privilege>();
>>>>        }
>>>>
>>>>        public virtual void AddPrivilege(Privilege privilege)
>>>>        {
>>>>            privilege.RolesWithThisPrivilege.Add(this);
>>>>            Privileges.Add(privilege);
>>>>        }
>>>>    }
>>>>
>>>>    public class Privilege : IAggregateRoot
>>>>    {
>>>>        public virtual int Id { get; private set; }
>>>>        public virtual string Path { get; set; } //
>>>> ModuleName.Function
>>>>        public virtual IList<Role> RolesWithThisPrivilege { get;
>>>> private set; }
>>>>
>>>>        public Privilege()
>>>>        {
>>>>            RolesWithThisPrivilege = new List<Role>();
>>>>        }
>>>>    }
>>>>
>>>>
>>>> Here follows the mapping class code:
>>>>
>>>>    public class RoleMap : ClassMap<Model.Entities.Role>
>>>>    {
>>>>        public RoleMap()
>>>>        {
>>>>            Id(x => x.Id);
>>>>            Map(x => x.Name).Not.Nullable();
>>>>            HasManyToMany(x => x.Privileges)
>>>>                .Cascade.All()
>>>>                .Table("RolePrivilege");
>>>>            Table("roles");
>>>>        }
>>>>    }
>>>>
>>>>    public class PrivilegeMap : ClassMap<Model.Entities.Privilege>
>>>>    {
>>>>        public PrivilegeMap()
>>>>        {
>>>>            Id(x => x.Id);
>>>>            Map(x => x.Path).Not.Nullable();
>>>>            HasManyToMany(x => x.RolesWithThisPrivilege)
>>>>                .Cascade.All()
>>>>                .Inverse()
>>>>                .Table("RolePrivilege");
>>>>            Table("privileges");
>>>>        }
>>>>    }
>>>>
>>>> Now the code that populates the tables:
>>>>
>>>>        private void InsertDefaultRoles()
>>>>        {
>>>>            IRoleRepository role_rep =
>>>> _facade.GetRepository<RoleRepository, Role>();
>>>>            Role r;
>>>>            Privilege p = new Privilege();
>>>>            p.Path = "*.a";
>>>>            foreach (DefaultRoleNames name in Enum.GetValues(typeof
>>>> (DefaultRoleNames)))
>>>>            {
>>>>                r = new Role();
>>>>                r.Name = name;
>>>>                r.AddPrivilege(p); ;
>>>>                role_rep.Add(r);
>>>>            }
>>>>        }
>>>>
>>>> Upon execution what I get is:
>>>>
>>>> 1. Roles table gets populated with a bunch of roles
>>>> 2. Privileges table gets populated with the only privilege created so
>>>> far
>>>>
>>>> The join table specified in the mapping class (RolePrivilege) is
>>>> empty, thus, if I load a
>>>> persisted role the assigned priviles list is empty.
>>>>
>>>> What's that I'm doing the wrong way?
>>>>
>>>> Thanks in advance for your help.
>>>> Regards,
>>>> Carmine
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> - Hudson
>>> http://www.bestguesstheory.com
>>> http://twitter.com/HudsonAkridge
>>>
>>
>>
>>
>> --
>> - Hudson
>> http://www.bestguesstheory.com
>> http://twitter.com/HudsonAkridge
>>
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to