当我定义一个名为“EntityBase”的抽象类时,我正在学习NHibernate:
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中编写这种无聊的公共属性,但是我可以在哪里将这些公共属性放在一个文件中,以便每个映射文件都可以引用它?
答案 0 :(得分:0)
我猜你的“常用属性”是指基类中的属性?在NH中映射继承有不同的策略。在这里阅读更多...... http://nhibernate.info/doc/nh/en/index.html#inheritance
答案 1 :(得分:0)
使用Fluent NHibernate很容易,我鼓励使用它而不是XML映射。如果您坚持使用XML,请参阅my answer to this question。