nhibernatehilo

NHibernate, HiLo and many-to-many association


I have two entities, Role and Permission, each with its table in the database and properly set-up ID generation with a HiLo algorithm. This works fine. However, there is one more table in the database, ROLE_PERMISSION_ASSIGNMENT, simply containing foreign keys to the two forementioned tables, binding the entities together. This table does not have a entity counterpart in my application.

The mapping for the Role entity looks like this:

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
        Table("\"ROLE\"");
        LazyLoad();            
        Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_role", "50");           
        Map(x => x.Name).Column("name");
        HasManyToMany<Permission>(x => x.Permissions)
           .Table("\"ROLE_PERMISSION_ASSIGNMENT\"")
           .ParentKeyColumn("fk_id_role")
           .ChildKeyColumn("fk_id_permission")
           .Cascade.None();
    }
}

Since I do not have an entity for ROLE_PERMISSION_ASSIGNMENT table, I can't specify the way its ID should be generated and thus when saving a Role (containing some Permissions) in the DB, it fails while creating the corresponding entries in ROLE_PERMISSION_ASSIGNMENT, because it does not provide a primary key.

Is there a way to tell NHibernate to generate IDs for ROLE_PERMISSION_ASSIGNMENT also with the HiLo algorithm?

Thank you very much.


Solution

  • You need to map that association as an idbag, which does exactly what you want (see http://www.nhforge.org/doc/nh/en/index.html#collections-idbag)

    I don't think Fluent supports it; you'll have to mix in XML mapping for that.