1

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.

twoflower
  • 6,788
  • 2
  • 33
  • 44
  • usually an association table has only two fields which are also used as a composite PK. can you explain why you need another kind of ID for it? – J. Ed Jun 29 '11 at 16:49
  • Thank you sJhonny, you are right, there is no need to have an additional ID in an association table, good point. – twoflower Jul 02 '11 at 14:52

1 Answers1

0

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.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Thank you Diego! This indeed accomplishes what I intended to do. On the other hand, after reading sJhonny's comment, I rethought it again and realized that I probably don't really need the synthetic ID in such kind of table. Your answer is very helpful, though. – twoflower Jul 02 '11 at 14:51