NHibernate ORM of Inherit Tree

NHibernate ORM of Inherit Tree
 
UML:
public abstract class GenDocument
public sealed class Document : GenDocument
public sealed class NoticeDocument : GenDocument

 
Mapping Define:
<class name="GenDocument" table="GenDocs">
    <id name="ID" column="OBID">
        <generator class="uuid.hex" />
    </id>
    <timestamp name="LastUpdated" />
    <property name="DocumentName" column="DocumentName" />
    <property name="Revision" column="Revision" />
    <property name="Sequence" column="Sequence" />
    <property name="Workflow" column="Workflow" />
    <property name="OwnerName" column="OwnerName" />
    <property name="LcStatus" column="LcStatus" />
    <property name="CreatedBy" column="CreatedBy" />
    <property name="CreatedDate" column="CreatedDate" />
    <property name="UpdatedBy" column="UpdatedBy" />
</class>
 
<class name="Document" table="Documents">
    <id name="ID" column="OBID">
        <generator class="uuid.hex" />
    </id>
    <timestamp name="LastUpdated" />
    <property name="DocumentName" column="DocumentName" />
    <property name="Revision" column="Revision" />
    <property name="Sequence" column="Sequence" />
    <property name="Workflow" column="Workflow" />
    <property name="OwnerName" column="OwnerName" />
    <property name="LcStatus" column="LcStatus" />
    <property name="CreatedBy" column="CreatedBy" />
    <property name="CreatedDate" column="CreatedDate" />
    <property name="UpdatedBy" column="UpdatedBy" />
</class>
 
<class name="NoticeDocument" table="NoticeDocuments">
    <id name="ID" column="OBID">
        <generator class="uuid.hex" />
    </id>
    <timestamp name="LastUpdated" />
    <property name="DocumentName" column="DocumentName" />
    <property name="Revision" column="Revision" />
    <property name="Sequence" column="Sequence" />
    <property name="Workflow" column="Workflow" />
    <property name="OwnerName" column="OwnerName" />
    <property name="LcStatus" column="LcStatus" />
    <property name="Content" column="Content" />
    <property name="CreatedBy" column="CreatedBy" />
    <property name="CreatedDate" column="CreatedDate" />
    <property name="UpdatedBy" column="UpdatedBy" />
</class>
 
Code:
 
IDaoFactory factory = GetDaoFactory();
 
/* create an transient object of Document */
Document document = new Document();
document.DocumentName = "document #1";
document.Revision = "A";
document.Sequence = 1;
document.OwnerName = "alan";
document.CreatedBy = "alan";
 
/* persist the Document object */
IDocumentDao documentDao = factory.GetDocumentDao();
documentDao.Save(document);
documentDao.Commit();
documentDao.Dispose();
 
/* create an transient object of NoticeDocument */
NoticeDocument noticeDocument = new NoticeDocument();
noticeDocument.DocumentName = "notice #1";
noticeDocument.Revision = "A";
noticeDocument.Sequence = 1;
noticeDocument.OwnerName = "alan";
noticeDocument.CreatedBy = "alan";
noticeDocument.Content = "notice body of notice #1";
 
/* persist the NoticeDocument object */
INoticeDocumentDao noticeDocumentDao = factory.GetNoticeDocumentDao();
noticeDocumentDao.Save(noticeDocument);
noticeDocumentDao.Commit();
noticeDocumentDao.Dispose();
 
/* Load All by GenDocument Data Access Object */
IGenDocumentDao genDocumentDao = factory.GetGenDocumentDao();
IList genDocs = genDocumentDao.GetAll();
genDocs.Dispose();
/*
 * GenDocument的数据访问函数应该在GetAll函数中返回所有
 * 它本身的对象以及其子类的对象
 */
Assert.AreEqual(2, genDocs.Count);
 
/* Load All by Document Data Access Object */
documentDao = factory.GetDocumentDao();
IList documents = documentDao.GetAll();
documentDao.Dispose();
/*
 * Document的数据访问函数应该在GetAll函数中返回所有
 * 它本身的对象,建议将Document class定义为sealed
 * 避免继承它
 */
Assert.AreEqual(1, documents.Count);
Assert.AreEqual("document #1", ((Document)documents[0]).DocumentName);
 
/* Load All by NoticeDocument Data Access Object */
noticeDocumentDao = factory.GetNoticeDocumentDao();
IList noticeDocs = noticeDocumentDao.GetAll();
noticeDocumentDao.Dispose();
/*
 * NoticeDocument的数据访问函数应该在GetAll函数中返回所有
 * 它本身的对象,建议将NoticeDocument class定义为sealed
 * 避免继承它
 */
Assert.AreEqual(1, noticeDocs.Count);
Assert.AreEqual("notice body of notice #1", ((NoticeDocument)noticeDocs[0]).Content);
原文地址:https://www.cnblogs.com/hcfalan/p/439380.html