NHibernate,HiLo和多对多协会 [英] NHibernate, HiLo and many-to-many association

查看:86
本文介绍了NHibernate,HiLo和多对多协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,角色权限,每个实体在数据库中都有其表,并使用HiLo算法正确设置了ID.这很好.但是,数据库中还有另外一个表 ROLE_PERMISSION_ASSIGNMENT ,其中仅包含指向上述两个表的外键,将实体绑定在一起.该表在我的应用程序中没有实体副本.

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.

角色实体的映射如下:

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();
    }
}

由于我没有 ROLE_PERMISSION_ASSIGNMENT 表的实体,因此我无法指定其ID的生成方式,因此在保存 Role (包含一些<数据库中的"strong>权限"),由于它不提供主键,因此在 ROLE_PERMISSION_ASSIGNMENT 中创建相应的条目时会失败.

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.

是否可以通过HiLo算法告诉NHibernate为 ROLE_PERMISSION_ASSIGNMENT 生成ID?

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

非常感谢您.

推荐答案

您需要将该关联映射为idbag,它恰好满足您的要求(请参见

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)

我不认为Fluent支持它.您必须为此混入XML映射.

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

这篇关于NHibernate,HiLo和多对多协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆