NHibernate联合主键详细示例

时间:2022-04-22
本文章向大家介绍NHibernate联合主键详细示例,主要内容包括一、数据表关系图、二、关键步骤、三、出错了,有Bug、四、终于实现了,总结、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

使用NHibernate实现一对多,多对一的关联很是简单,可如果要用复合主键实现确实让人有些淡淡的疼。虽然很淡疼但还是要去抹平这个坑,在下不才,愿意尝试。

以示例进入正文,源码下载地址

一、数据表关系图

很明显,他是一个自引用数表,实现无限级树结构的存储。

二、关键步骤

  • 注解如何实现复合主键

根据官方文档说明,联合主键最好是一个独立的类,需要重载Equals和GetHashCode方法,且标记为可序列化。代码如下:

[Serializable]
public class BaseInfo
{
    public virtual string Id { get; set; }
    public virtual string GroupNumber { get; set; }

    public override bool Equals(object obj)
    {
        var baseInfo = obj as BaseInfo;
        if (baseInfo == null)
        {
            return false;
        }

        return baseInfo.Id == this.Id && baseInfo.GroupNumber == this.GroupNumber;
    }
    public override int GetHashCode()
    {
        return base.GetHashCode(); 
    }
}
  • 子类配置好联合主键
[CompositeId(0, Name = "BN")]
[KeyProperty(1, Name = "Id", Column = "Id", TypeType = typeof(string))]
[KeyProperty(2, Name = "GroupNumber", Column = "GroupNumber", TypeType = typeof(string))]
public virtual BaseInfo BN { get; set; }

说明: 1.实现为引用BaseInfo类,而不是继承.

  • 实现一对 和 多对一的映射

这步没有多大难度,主要处理好注解的顺序即可,以及OneToMany时联合主键如何设置的问题.示例代码如下:

[Bag(0, Name = "Childs", Cascade = "all", Lazy = CollectionLazy.False, Inverse = true)]
[Key(1)]
[Column(2, Name = "ParentId")]
[Column(3, Name = "GroupNumber")]
[OneToMany(4, ClassType = typeof(Foo))]
public virtual IList<Foo> Childs { get; set; }

[ManyToOne(0, Name = "Parent", ClassType = typeof(Foo))] 
[Column(1, Name = "ParentId")]
[Column(2, Name = "GroupNumber")]
public virtual Foo Parent { get; set; }

三、出错了,有Bug

  • childs没有数据

重载的GetHashCode方法有问题,返回值应该是联合主键HashCode,优化后的实现如下:

public override int GetHashCode()
{
    return (this.Id + "|" + this.GroupNumber).GetHashCode(); //判断缓存是否存在,已此作为Key
}
  • 插入数据时报错,提示SqlParameterCollection的索引无效[索引溢出错误]

原因,最初在设计Parent的时候,与联合主键共用了一个字段GroupNumber,导致在NHibernate做映射转换的时候会多计算出一个需要填充的值,但SqlParameterCollection中又少一个位置。优化代码如下:

//外键与联合主键不要共用字段
[ManyToOne(0, Name = "Parent", ClassType = typeof(Foo))] 
[Column(1, Name = "ParentId")]
[Column(2, Name = "ParentGroupNumber")]
public virtual Foo Parent { get; set; }

说明: 1.由于联合外键与联合主键共用了一个字段,导致映射出错

四、终于实现了,总结

  • 类都必须可以序列化,也就是要还serializable标注
  • 继承BaseInfo实现联合主键(不推荐使用)

在Save时,如果用session.merge方法组合缓存与修改对象,返回值的主键会为Null

  • 联合主键与联合外键字段不能重复,也不能共用
  • 注意重载的GetHashCode和Equals方法
  • GetHashCode返回实例的惟一标识
  • Equals判断是否相同实例的具体实现