您的位置:首页 > 移动开发

NHibernate中实体类、Mapping配置文件与数据库表之间的关系

2017-06-16 17:03 435 查看
NHibernate是一个O/RM框架在其应用中会产生三部分内容:实体类、Mapping配置文件、数据库表。这三者的联结促成了NHibernate的实现的基础。在这三者中Mapping配置文件为实体类和数据库表的纽带。没有Mapping配置文件NHibernate无法根据实体类生成数据库表,无法形成对象与数据库表的映射。实体类和数据库表是相互独立的二者不知道对方的存在,中间任何的交互都是通过Mapping配置文件来连接的。

Mapping配置文件直接决定了数据库表生成时把实体类的哪些属性作为对应的列。在生成数据库表时数据库表的结构只与每个Mapping配置文件直接相关,而与实体类间接相关。无论实体类的类结构是如何设计的,只要实体类中的属性能够在Mapping配置文件中找到对应配置项就可以在数据库表中生成对应的列;不管该属性是在哪里,无论父类中还是接口中。

实体类中除了与数据库表的列相对应的属性外还可以有额外的属性,只要没有在Mapping配置文件中进行配置NHibernate在进行增删改查等操作时并不会有影响而报错。

数据库和数据库表只是为数据提供存储位置而不会影响程序。一个示例。

1、基类

public class BaseDomain
{
//因NHibernate启用了延迟加载,所以属性需要加上virtual。可在*.hbm.xml中关闭。

/// <summary>
/// ID
/// </summary>
public virtual Guid ID { get; set; }

/// <summary>
/// 当前页码
/// </summary>
public virtual int PageIndex { get; set; }

/// <summary>
/// 页面显示的条数
/// </summary>
public virtual int PageSize { get; set; }
}

2、子类(实体类)
/// <summary>
/// 商品
/// </summary>
public class Product:BaseDomain
{
#region Propertys
///// <summary> 移动到了父类中
///// ID
///// </summary>
//public virtual Guid ID { get; set; }

/// <summary>
/// 编号
/// </summary>
public virtual string Code { get; set; }

/// <summary>
/// 名称
/// </summary>
public virtual string Name { get; set; }

/// <summary>
/// 规格
/// </summary>
public virtual string QuantityPerUnit { get; set; }

/// <summary>
/// 单位
/// </summary>
public virtual string Unit { get; set; }

/// <summary>
/// 售价
/// </summary>
public virtual decimal SellPrice { get; set; }

/// <summary>
/// 进价
/// </summary>
public virtual decimal BuyPrice { get; set; }

/// <summary>
/// 备注
/// </summary>
public virtual string Remark { get; set; }

/// <summary>
/// 商家/供应商ID
/// </summary>
public virtual System.Guid BusinessID
{
get;

set;
}
#endregion
}

3、Mapping配置文件
<?xml version="1.0" encoding="utf-8" ?>

<!--assembly="NHibernateDomain"对应实体类所在的程序集,namespace="NHibernateDomain.Domain"对应实体类所在的命名空间-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDomain" namespace="NHibernateDomain.Domain">
<class name="Product" table="T_Product" lazy="true" >
<id name="ID" column="ID" type="Guid" >
<generator class="assigned" />
</id>

<property name="Code" type="string">
<column name="Code" length="50"/>
</property>

<property name="Name" type="string">
<column name="Name" length="50"/>
</property>

<property name="QuantityPerUnit" type="string">
<column name="QuantityPerUnit" length="50"/>
</property>

<property name="Unit" type="string">
<column name="Unit" length="50"/>
</property>

<property name="SellPrice" type="decimal">
<column name="SellPrice" precision="14" scale="2"/>
</property>

<property name="BuyPrice" type="decimal">
<column name="BuyPrice" precision="14" scale="2"/>
</property>

<property name="Remark" type="string">
<column name="Remark" length="200"/>
</property>

<property name ="BusinessID" type ="Guid">
<column name="BusinessID" length="36"/>
</property>
</class>
</hibernate-mapping>

注:父类中的pageIndex,pageSize属性并不会在数据库表中生成对应的列,实体类的ID属性是从父类继承而来在Mapping配置文件中有对应配置项是可以在数据库表中生成对应列的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐