NHibernate, HiLo and many-to-many association

僤鯓⒐⒋嵵緔 提交于 2019-12-11 12:36:00

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6524064/nhibernate-hilo-and-many-to-many-association

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!