具有两个自然键的多对多关系

时间:2013-02-07 19:21:32

标签: nhibernate

我有以下映射

UserProfile.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="AngusBook.Domain"
                   namespace="AngusBook.Domain">

  <class name="UserProfile">
    <id name="UserId" type="int">
      <generator class="identity" />
    </id>
    <natural-id mutable="false">
      <property name="UserName" />
    </natural-id>
    <set name="Companies" table="Users_Companies">
      <key column="UserId"/>
      <many-to-many column="CompanyId" class="Company" />
    </set>
  </class>

</hibernate-mapping>

Company.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="AngusBook.Domain" namespace="AngusBook.Domain" >

  <class name="Company">
    <id name="Id">
      <generator class="hilo" />
    </id>
    <natural-id mutable="false">
      <property name="CompanyName" />
    </natural-id>
    <set name="Users" table="Users_Companies">
      <key column="CompanyId"/>
      <many-to-many column="UserId" class="UserProfile" />
    </set>
  </class>

</hibernate-mapping>

表格设计 enter image description here

通过这种映射,我可以在Users_Companies表中有两个相同的行(即:两行具有属于UserProfile和Company表的同一对外键)。 使用映射,当尝试将一对外键插入表中已存在的Users_Companies时,如何导致NHibernate或SQL抛出错误/异常?我希望每一行都在Users_Companies是唯一的,没有重复数据。

1 个答案:

答案 0 :(得分:0)

我发现了问题。我没有正确实现Equals()和GetHashCode()。

我最终重构了我的实体以使用以下基本实体类并修复了该问题。将重复实体添加到集合时不会引发任何错误,但该集合将不会添加重复的实体。

<强> Entity.cs     公共抽象类实体         {

        public virtual TId Id { get; protected set; }
        protected virtual int Version { get; set; }

        public override bool Equals(object obj)
        {
            return Equals(obj as Entity<TId>);
        }

        private static bool IsTransient(Entity<TId> obj)
        {
            return obj != null &&
                   Equals(obj.Id, default(TId));
        }

        private Type GetUnproxiedType()
        {
            return GetType();
        }

        public virtual bool Equals(Entity<TId> other)
        {
            if (other == null)
                return false;

            if (ReferenceEquals(this, other))
                return true;

            if (!IsTransient(this) &&
                !IsTransient(other) &&
                Equals(Id, other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return thisType.IsAssignableFrom(otherType) ||
                       otherType.IsAssignableFrom(thisType);
            }

            return false;
        }

        public override int GetHashCode()
        {
            if (Equals(Id, default(TId)))
                return base.GetHashCode();
            return Id.GetHashCode();
        }

    }

    public abstract class Entity : Entity<Guid>
    {

    }

<强> UserProfile.cs

 public class UserProfile : Entity<int>
    {
        public UserProfile()
        {
            Companies = new HashedSet<Company>();
        }

        public virtual string UserName { set; get; }
        public virtual ISet<Company> Companies { set; get; } 
    }

<强> Company.cs

public class Company : Entity<int>
    {
        public Company()
        {
            Users = new HashedSet<UserProfile>();
        }
        public Company(string name) :this()
        {
            this.CompanyName = name;
        }

        public virtual string CompanyName { set; get; }

        public virtual ISet<UserProfile> Users { set; get; }

      }