十、NHIbernate之使用视图

一、新建视图

create view ViewCustomer

as

(

      select

           c.firstName+c.lastName as fullName, o.orderId, o.orderDate, count(op.product) as productNum

      from

           Customer c

         join [order] o          on c.customerId=o.customer

         join OrderProduct op           on op.[Order]=o.OrderId

         join Product p          on p.ProductId=op.Product

      group by

           c.firstName+c.lastName, o.orderId, o.orderDate

)

Go

 

二、编写持久化类

新建ViewCustomer.Cs类文件,并编写代码如下:

namespace Model.Entities

{

    public class ViewCustomer

    {

        public virtual Int32 CustomerId { get; set; }

        public virtual string FullName { get; set; }

        public virtual Int32 OrderId { get; set; }

        public virtual DateTime OrderDate { get; set; }

        public virtual Int32 ProductNum { get; set; }

    }

}

 

三、编写映射文件

新建ViewCustomer.hbm.xml文件,并编写代码如下:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.Entities">

    <class name="Model.Entities.ViewCustomer,Model" table="ViewCustomer" mutable="false"><!--mutable="false"——对对象的修改不能持久化到数据库中-->

        <id name="CustomerId" column="CustomerId" type="Int32">

            <generator class="native"/>

        </id>

        <property name="FullName" column="FullName" type="string"/>

        <property name="OrderId" column="OrderId" type="Int32"/>

        <property name="OrderDate" column="OrderDate" type="DateTime"/>

        <property name="ProductNum" column="ProductNum" type="Int32"/>

    </class>

</hibernate-mapping>

(注意:我练习时在这里卡到了,因为程序老是报ViewCustomer is not mapped的错误,我一行行地检查代码,怎么都发现不了错误,最后脑子一闪光发现居然是因为我把ViewCustomer.hbm.xml文件建成了ViewCustomer.xml文件,而且忘记了将其属性设置为内嵌的,惭愧惭愧,居然在这里搞了这么久,忘以后提起精神吸取教训,不再犯同样的错误)

 

四、编写DAL中的读取函数

代码如下:

        //读取视图

        public IList<ViewCustomer> GetViewCustomer()

        {

            return session.CreateQuery("from ViewCustomer").List<ViewCustomer>();

        }

 

五、编写DAL.Test中的测试函数

代码如下:

        [Test]

        public void GetViewCustomerTest()

        {

            IList<ViewCustomer> viewCustomers = sample.GetViewCustomer();

        }

 

然后设置断点,并调试测试,在即时窗口中查看数据是否正确,下面是我执行的结果:

viewCustomers.Count

2

viewCustomers[0].FullName

"luojun"

viewCustomers[1].FullName

"soldierluo"

 

六、对对象的修改不能持久化到数据库中

对于视图对象,我们即使修改了其中的数据也无法更新到数据库中,因为我们在映射文件中的class配置节配置了mutable="false" 属性,下面我们来做个试验:

修改GetViewCustomer()函数如下:

        //读取视图

        public IList<ViewCustomer> GetViewCustomer()

        {

            ViewCustomer vc = session.Get<ViewCustomer>(1);

            vc.OrderDate = DateTime.Now;

            session.Update(vc);

 

            return session.CreateQuery("from ViewCustomer").List<ViewCustomer>();

        }

然后设置断点调试测试,并在即时窗口中查看数据,我的如下:

viewCustomers.Count

2

viewCustomers[0].CustomerId+"_"+viewCustomers[0].FullName+"_"+viewCustomers[0].OrderId+"_"+viewCustomers[0].OrderDate.ToString()+"_"+viewCustomers[0].ProductNum

"1_luojun_1_2009-10-25 14:27:00_1"

viewCustomers[1].CustomerId+"_"+viewCustomers[1].FullName+"_"+viewCustomers[1].OrderId+"_"+viewCustomers[1].OrderDate.ToString()+"_"+viewCustomers[1].ProductNum

"2_soldierluo_2_2009-10-17 11:22:46_2"

 

上面我们可以看到,函数中我们修改了CustomerId1ViewCustomerOrderDate,返回的结果显示,这时间确实被修改了(注意结果中的绿色部分),难道上面的设置是没用的吗?赶紧到数据库里去查查看。

数据库的结果没变啊!!!这是怎么回事,我想大概是NHibernate对查询的优化吧!难道它也不考虑下执行是否成功,就直接一竿子优化?在此请特别注意,这里:

一是证明了mutable="false" 属性的设置是有效的

二是提醒我们必须在处处与数据库相关的数据操作时使用事务(这个还有待验证,但是使用事务是没错的)

这里有个问题就是,对于视图不能更新,但是进行更新操作它也不会报错,很郁闷,希望有高人指点

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值