0

我正在学习 NHibernate,当我定义了一个名为“EntityBase”的抽象类时:

public abstract class EntityBase
{
    public EntityBase()
    {
        CreateTime = DateTime.Now;
        IsDelete = false;
    }
    public virtual int Id { get; set; }

    public virtual string Description { get; set; }

    public virtual DateTime CreateTime { get; protected set; }

    public virtual bool IsDelete { get; set; }

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

像这样的一些子类:

public class Role : EntityBase, IAggregateRoot
{
    public virtual string Name { get; set; }

    public virtual IList<User> Users { get; set; }

    public virtual IList<Permission> Permissions { get; set; }
}

public class Department : EntityBase, IAggregateRoot
{
    public virtual string Name { get; set; }

    public virtual IList<User> Users { get; set; }
}

我会在下面写很多 *.hbm.xml 文件:

<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="LY.OA.Model.Authority" assembly="LY.OA.Model">
      <class name="Role" table="T_Role" lazy="false">
<id name="Id" column="Id" type="int">
  <generator class="native"></generator>
</id>
<property name="Name">
  <column name="RoleName" sql-type="varchar(30)" not-null="true"></column>
</property>

<bag name="Users" table="T_UserRole" inverse="true" cascade="all" lazy="true">
  <key column="RoleId"></key>
  <many-to-many class="User" column="UserId"/>
</bag>

<!--Common properties-->
<property name="Description" column="DESCRIPTION" type="String"/>
<property name="CreateTime" column="CREATETIME" type="DateTime"/>
<property name="IsDelete" column="ISDELETE" type="Boolean"/>
<property name="Version" column="VERSION" />

  </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="LY.OA.Model.Authority"   assembly="LY.OA.Model">
  <class name="Department" table="T_Department" lazy="false">
    <id name="Id" column="Id" type="int">
      <generator class="native"></generator>
    </id>
<property name="Name">
  <column name="DepartmentName" sql-type="varchar(30)" not-null="true"></column>
</property>

<!--Common properties-->
<property name="Description" column="DESCRIPTION" type="String"/>
<property name="CreateTime" column="CREATETIME" type="DateTime"/>
<property name="IsDelete" column="ISDELETE" type="Boolean"/>
<property name="Version" column="VERSION" />

  </class>
</hibernate-mapping>

但是,我不会在每个xml中编写这么无聊的通用属性,但是我可以将这些通用属性放在文件中的哪里,以便每个映射文件都可以引用它呢?

4

2 回答 2

0

我猜您的“通用属性”是指基类中的属性?在 NH 中映射继承有不同的策略。在这里阅读更多... http://nhibernate.info/doc/nh/en/index.html#inheritance

于 2013-10-30T17:09:19.270 回答
0

使用 Fluent NHibernate 很容易,我鼓励使用它而不是 XML 映射。如果您对 XML 感到困惑,请参阅我对这个问题的回答

于 2013-10-30T17:25:19.743 回答