NHibernate使用复合主键(二)(包含一对多关系)

接着使用复合主键(1)继续:

还是那三张表哟!

Users表(用户信息表):

   UserID char(10) not null primary key,

         UserName varchar(50)

Books表(图书信息表):

  BSN char(10) not null primary key,

        BookName  varchar(50)

BorrowRecord表(图书借还登记表):

  UserID char(10) not null,

        BSN char(10) not null,

        BorrowDate DateTime not null,

        ReturnDate DateTime,

  constraint PK_Library primary key (UserID,BSN,BorrowDate)

Users.UserID 对于 BorrowRecord.UserID 是 one-to-many

Books.BSN 对于 BorrowRecord.BSN 是 one-to-many

==============================================================================

Users表

namespace Model2
{
#region User

/// <summary>
/// User object for NHibernate mapped table 'Users'.
/// </summary>
public class User
{
#region Member Variables

protected string _id;
protected string _userName;
        protected IList _borrowRecord;

#endregion

#region Constructors

public User() { }

public User( string userName )
{
   this._userName = userName;
}

#endregion

#region Public Properties

public virtual string Id
{
   get {return _id;}
   set
   {
    if ( value != null && value.Length > 10)
     throw new ArgumentOutOfRangeException("Invalid value for Id", value, value.ToString());
    _id = value;
   }
}

public virtual string UserName
{
   get { return _userName; }
   set
   {
    if ( value != null && value.Length > 50)
     throw new ArgumentOutOfRangeException("Invalid value for UserName", value, value.ToString());
    _userName = value;
   }
}

        public virtual IList BorrowRecord
        {
            get
            {
                if (_borrowRecord == null)
                {
                    _borrowRecord = new ArrayList();
                }
                return _borrowRecord;
            }
            set { _borrowRecord = value; }
        }

       

#endregion
}
#endregion
}

==============================================================================

User的xml映射文件(User.hbm.xml):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model2.User, Model2" table="Users">
<id name="Id" type="String" unsaved-value="null">
   <column name="UserID" length="10" sql-type="char" not-null="true" unique="true" index="PK_Users"/>
   <generator class="assigned" />
</id>
<property name="UserName" type="String">
   <column name="UserName" length="50" sql-type="varchar" not-null="false"/>
</property>
    <bag name="BorrowRecord" inverse="true" lazy="true" cascade="all-delete-orphan">
      <key column="UserID"/>
      <one-to-many class="Model2.BorrowRecord, Model2"/>
    </bag>
</class>
</hibernate-mapping>

==============================================================================

Books类:

namespace Model2
{
#region Book

/// <summary>
/// Book object for NHibernate mapped table 'Books'.
/// </summary>
public class Book
{
#region Member Variables

protected string _id;
protected string _bookName;
        protected IList _borrowRecord;

#endregion

#region Constructors

public Book() { }

public Book( string bookName )
{
   this._bookName = bookName;
}

#endregion

#region Public Properties

public virtual string Id
{
   get {return _id;}
   set
   {
    if ( value != null && value.Length > 10)
     throw new ArgumentOutOfRangeException("Invalid value for Id", value, value.ToString());
    _id = value;
   }
}

public virtual string BookName
{
   get { return _bookName; }
   set
   {
    if ( value != null && value.Length > 50)
     throw new ArgumentOutOfRangeException("Invalid value for BookName", value, value.ToString());
    _bookName = value;
   }
}

        public virtual IList BorrowRecord
        {
            get
            {
                if (_borrowRecord == null)
                {
                    _borrowRecord = new ArrayList();
                }
                return _borrowRecord;
            }
            set { _borrowRecord = value; }
        }
        #endregion
}
#endregion
}

==============================================================================

Books的xml映射文件(Book.hbm.xml):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model2.Book, Model2" table="Books">
<id name="Id" type="String" unsaved-value="null">
   <column name="BSN" length="10" sql-type="char" not-null="true" unique="true" index="PK_Books"/>
   <generator class="assigned" />
</id>
<property name="BookName" type="String">
   <column name="BookName" length="50" sql-type="varchar" not-null="false"/>
</property>
    <bag name="BorrowRecord" inverse="true" lazy="true" cascade="all-delete-orphan">
      <key column="BSN"/>
      <one-to-many class="Model2.BorrowRecord, Model2"/>
    </bag>
</class>
</hibernate-mapping>

==============================================================================

BorrowRecord类:

namespace Model2
{
#region BorrowRecord

/// <summary>
/// BorrowRecord object for NHibernate mapped table 'BorrowRecord'.
/// </summary>

    //[Serializable]
    public class BorrowRecord
{
#region Member Variables

        protected BorrowRecordPK borrowPK;
        protected DateTime _returnDate;
      

#endregion

#region Constructors

public BorrowRecord() { }

public BorrowRecord( DateTime returnDate )
{
   this._returnDate = returnDate;
}

#endregion

#region Public Properties


public virtual DateTime ReturnDate
{
   get { return _returnDate; }
   set { _returnDate = value; }
}

        public virtual BorrowRecordPK BorrowPK
        {
            set
            {
                borrowPK = value;
            }
            get
            {
                return borrowPK;
            }
        }
             
        #endregion
}
#endregion
}

==============================================================================

BorrowRecord复合主键类:

namespace Model2
{
   
    [Serializable]
    public class BorrowRecordPK
    {
        protected Book _book;
        protected User _user;
      

        public BorrowRecordPK()
        {
        }

        public virtual User User
        {
            set { _user = value; }
            get { return _user; }
       
        }

        public virtual Book Book
        {
            set { _book = value; }
            get { return _book; }

        }

        private DateTime borrowDate;

        public virtual DateTime BorrowDate
        {
            set
            {
                borrowDate = value;
            }
            get
            {
                return borrowDate;
            }
        }
            
        /// <summary>
        /// 判断两个对象是否相同,这个方法需要重写
        /// </summary>
        /// <param name="obj">进行比较的对象</param>
        /// <returns>真true或假false</returns>
        public override bool Equals(object obj)
        {
            if (obj is BorrowRecordPK)
            {
                BorrowRecordPK second = obj as BorrowRecordPK;
                if (this._user == second._user
                     && this._book == second._book
                     && this.BorrowDate == second.BorrowDate)
                {
                    return true;
                }
                else return false;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

    }
}

==============================================================================


BorrowRecord的xml映射文件(BorrowRecord.hbm.xml):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model2.BorrowRecord, Model2" table="BorrowRecord">
<composite-id name="BorrowPK" class="Model2.BorrowRecordPK, Model2">
        <key-many-to-one name="User" class="Model2.User, Model2" column="UserID" />
        <key-many-to-one name="Book" class="Model2.Book, Model2" column="BSN" />
        <key-property name="BorrowDate" />
</composite-id>
<property name="ReturnDate" type="DateTime">
   <column name="ReturnDate" length="8" sql-type="datetime" not-null="false"/>
</property>
</class>
</hibernate-mapping>

 

主要要注意的是:

1、使用了复合主键类要重写Equals()和GetHashCode()方法。

2、要将该复合类Serializable.

3、xml映射文件要设置为嵌入的资源。

你可能感兴趣的:(Hibernate)