目录
写在前面
前面的文章主要讲了对物理数据表的操作,当然了Nhibernate同样可以操作视图,本文将讲nhibernate对视图操作的种种。
文档与系列文章
[NHibernate]持久化类(Persistent Classes)
[NHibernate]集合类(Collections)映射
[NHibernate]缓存(NHibernate.Caches)
[NHibernate]NHibernate.Tool.hbm2net
[NHibernate]Nhibernate如何映射sqlserver中image字段
[NHibernate]条件查询Criteria Query
视图
首先创建一个VW_CustomerOrder(采用同数据表相同的命名规则,如数据表TB_Customer)的视图,选中CustomerID,CustomerName,CustomerAddress,OrderID,OrderDate字段
一个例子
添加视图的持久化类
namespace Wolfy.Shop.Domain.Entities
{
/// <summary>
/// 描述:客户订单视图持久化类
/// 创建人:wolfy
/// 创建时间:2014-11-08
/// </summary>
public class CustomerOrderView
{
/// <summary>
/// 客户id
/// </summary>
public virtual Guid CustomerID { get;private set; }
/// <summary>
/// 客户姓名
/// </summary>
public virtual string CustomerName { get; private set; }
/// <summary>
/// 客户住址
/// </summary>
public virtual string CustomerAddress { get; private set; }
/// <summary>
/// 订单id
/// </summary>
public virtual Guid OrderID { private set; get; }
/// <summary>
/// 下单时间
/// </summary>
public virtual DateTime OrderDate {private set; get; }
}
}
注意:对视图最常用的是查询操作,视图是一张虚表,也就是只读的(数据的来源是来自物理表的)。所以这里将持久化类的属性设置为只读的(private set)就可以了,如果真想修改数据了,就通过修改物理表的数据。
编写映射文件CustomerOrderView.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<!--assembly:程序集,namespace:命名空间-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Wolfy.Shop.Domain" namespace="Wolfy.Shop.Domain.Entities">
<class name="Wolfy.Shop.Domain.Entities.CustomerOrderView,Wolfy.Shop.Domain" table="VW_CusomterOrder" mutable="false">
<!--主键-->
<id name="CustomerID" type="Guid" unsaved-value="null">
<column name="CustomerID" sql-type="uniqueidentifier" not-null="true" unique="true"/>
<generator class="assigned"></generator>
</id>
<property name="CustomerName" column ="CustomerName" type="string"
length="16" not-null="false" />
<property name ="CustomerAddress" column="CustomerAddress" type="string"
length="128" not-null="false" />
<property name="OrderID" column="OrderID" type="Guid" not-null="true"/>
<property name="OrderDate" column="OrderDate" type="DateTime" />
</class>
</hibernate-mapping>
注意:1,在nhibernate中使用视图的时候,最容易忽略的就是没有给视图指定主键,这种情况下很容易出错。
2,注意映射文件中class节点指定mutable="false"属性(对对象的修改不能持久化到数据库中)。
3,记得添加过映射文件,修改hbm.xml属性为嵌入资源
测试
获取某客户下面所有的订单信息。
/// <summary>
/// 根据客户id查询视图,获得客户信息,及下单信息。
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public IList<CustomerOrderView> GetCustomerOrderViewByCustomerId(Guid customerID)
{
ISession session = NHibernateHelper.GetSession();
return session.CreateCriteria(typeof(CustomerOrderView))
.Add(Restrictions.Eq("CustomerID", customerID))
.List<CustomerOrderView>();
}
此时会有一个异常
“NHibernate.InvalidProxyTypeException”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理 其他信息: The following types may not be used as proxies: Wolfy.Shop.Domain.Entities.CustomerOrderView: method set_CustomerID should be 'public/protected virtual' or 'protected internal virtual' Wolfy.Shop.Domain.Entities.CustomerOrderView: method set_CustomerName should be 'public/protected virtual' or 'protected internal virtual' Wolfy.Shop.Domain.Entities.CustomerOrderView: method set_CustomerAddress should be 'public/protected virtual' or 'protected internal virtual' Wolfy.Shop.Domain.Entities.CustomerOrderView: method set_OrderID should be 'public/protected virtual' or 'protected internal virtual' Wolfy.Shop.Domain.Entities.CustomerOrderView: method set_OrderDate should be 'public/protected virtual' or 'protected internal virtual'
意思已经很明白了,说属性的set方法,应该是Public或者Protect virtual的。解决办法,要么修改回默认的public,要么修改映射文件的默认加载方式将hibernate-mapping的default-lazy属性设置为false。让他立即加载。
<?xml version="1.0" encoding="utf-8" ?>
<!--assembly:程序集,namespace:命名空间-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Wolfy.Shop.Domain" namespace="Wolfy.Shop.Domain.Entities" default-lazy="false">
<class name="Wolfy.Shop.Domain.Entities.CustomerOrderView,Wolfy.Shop.Domain" table="VW_CusomterOrder" mutable="false">
<!--主键-->
<id name="CustomerID" type="Guid" unsaved-value="null">
<column name="CustomerID" sql-type="uniqueidentifier" not-null="true" unique="true"/>
<generator class="assigned"></generator>
</id>
<property name="CustomerName" column ="CustomerName" type="string"
length="16" not-null="false" />
<property name ="CustomerAddress" column="CustomerAddress" type="string"
length="128" not-null="false" />
<property name="OrderID" column="OrderID" type="Guid" not-null="true"/>
<property name="OrderDate" column="OrderDate" type="DateTime" />
</class>
</hibernate-mapping>
异常解决参考文章:http://blog.csdn.net/sunlihgt_love/article/details/4298131
结果
生成的sql语句
总结
本文介绍了在nhibernate中使用视图的内容,遇到一个bug,将解决bug的过程分享了一下,遇到bug不要慌,解决了一个bug,你就会对它印象深刻,至少下次再遇到了,你知道如何去解决了,也是一种进步,不要怕犯错。
参考文章:http://www.cnblogs.com/lyj/archive/2008/11/01/1324201.html