views:

76

answers:

1

Hi,

I have a many-to-many mapping defined (only relevant fields included) with FluentNHibernate (v1.0.0.637):

// MODEL:
public class User : IPersistentObject {
    public User() {
        Permissions = new HashedSet<Permission>();
    }
    public virtual int Id { get; protected set; }
    public virtual ISet<Permission> Permissions { get; set; }
}

public class Permission : IPersistentObject {
    public Permission() {
    }
    public virtual int Id { get; set; }
}

// MAPPING:
public class UserMap : ClassMap<User> {
    public UserMap() {
        Id(x => x.Id);
        HasManyToMany(x => x.Permissions).Cascade.All().AsSet();
    }
}

public class PermissionMap : ClassMap<Permission> {
    public PermissionMap() {
        Id(x => x.Id).GeneratedBy.Assigned();
        Map(x => x.Description);
    }
}

The following test fails as there is no record inserted into User_Permission table:

[Test]
public void AddingANewUserPrivilegeShouldSaveIt() {
    var p1 = new Permission {
        Id = 123,
        Description = "p1"
    };
    Session.Save(p1);

    var u = new User {
        Email = "[email protected]"
    };
    u.Permissions.Add(p1);
    Session.Save(u);

    var userId = u.Id;

    Session.Evict(u);
    Session.Get<User>(userId).Permissions.Should().Not.Be.Empty();
}

The SQL executed is (SQLite):

INSERT INTO "Permission" (Description, Id) VALUES (@p0, @p1);@p0 = 'p1', @p1 = 1
INSERT INTO "User" (Email) VALUES (@p0); select last_insert_rowid();@p0 = '[email protected]'
SELECT user0_.Id as Id2_0_, user0_.Email as Email2_0_ FROM "User" user0_ WHERE user0_.Id=@p0;@p0 = 1
SELECT permission0_.UserId as UserId1_, permission0_.PermissionId as Permissi2_1_, permission1_.Id as Id4_0_, permission1_.Description as Descript2_4_0_ FROM User_Permissions permission0_ left outer join "Permission" permission1_ on permission0_.PermissionId=permission1_.Id WHERE permission0_.UserId=@p0;@p0 = 1

We can clearly see that there is no record inserted into the User_Permissions table where it should be.

Not sure what I am doing wrong and need an advice.

So can you please help me to pass this test.

Thanks,
Dmitriy.

A: 

As miensol said in the comment, I have forgotten to commit the transaction.

Dmytrii Nagirniak