NHibernate也是能够操作存储过程的,不过第一次配置可能会碰到很多错误。

一、删除

  首先,我们新建一个存储过程如下:

  CREATE PROC DeletePerson
  @Id int
  AS
  DELETE FROM Person WHERE PersonId = @Id;

  修改映射文件,添加删除对象的存储过程:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.PersonModel, Model" table="Person">
<id name="PersonId" column="PersonId" type="Int32">
<generator class="native"/>
</id>
<property name="PersonName" column="PersonName" type="String"/>
<!--多对一关系:Person属于一个Country name是Person实体类里的-->
<many-to-one name="Country" column="CountryId" not-null="true" class="Model.CountryModel,Model" foreign-key="FK_Person_Country" />
<!-- 一个Country里面有多个Person -->
<set name="ListChild" table="Child" generic="true">
<key column="ParentId" foreign-key="FK_Child_Person"/>
<one-to-many class="Model.ChildModel,Model"/>
</set>
<sql-delete>DeletePerson ?</sql-delete>
</class>
</hibernate-mapping>

  执行存代码:

            using (ISession session = sessionFactory.OpenSession())
{
PersonModel p = session.Get<PersonModel>();
session.Delete(p);
session.Flush();
}

  从监控到,SQLServer执行的语句如下:

exec sp_executesql N'DeletePerson @p0',N'@p0 int',@p0=5

  明显已经看到哥写的存储过程名字。如果不是执行存储过程,则执行的语句如下:

exec sp_executesql N'DELETE FROM Person WHERE PersonId = @p0',N'@p0 int',@p0=5

  可以看到,如果你配置了存储过程,那么Delete()方法就执行存储过程,否则就执行NHibernate自己生成的SQL语句。

二、添加

   测试了一个NHibernate的添加存储过程之后,感觉就一个字,不爽,估计这种方式配置的存储也不会是好的选择,还是生成语句比较好。

  存储过程如下:

ALTER PROC InsertCountry
@CountryName nvarchar(50),
@CountryId int OUTPUT
AS
INSERT INTO Country VALUES(@CountryName);

  首先,添加将CountryId设置为自增的。

  然后配置文件如下(注意两个加粗的地方):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.CountryModel,Model" table="Country">
<id name="CountryId" column="CountryId" type="Int32">
<generator class="increment"/>
</id>
<property name="CountryName" column="CountryName" type="String"/>
<!-- 一个Country里面有多个Person -->
<set name="ListPerson" table="Person" generic="true">
<key column="CountryId" foreign-key="FK_Person_Country"/>
<one-to-many class="Model.PersonModel,Model"/>
</set>
<sql-insert>InsertCountry ?,?</sql-insert>
</class>
</hibernate-mapping>

  设置increment的目的是主键,本处为CountryId由NHibernate生成。实际上不是一个什么好的方法,因为NHibernate是通过SQL语句:

SELECT MAX(CountryId) FROM Country

  获得的。并且,你还必须设置数据库主键自增。一句话,不爽。都说了没什么人会选择这种方式咯。

  执行代码如下:

        static void Main(string[] args)
{
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory(); using (ISession session = sessionFactory.OpenSession())
{
CountryModel c = new CountryModel();
c.CountryName = "青州";
session.Save(c);
session.Flush();
}
Console.ReadKey();
}

  监控到SQL Server执行了:

exec sp_executesql N'InsertCountry @p0,@p1',N'@p0 nvarchar(4000),@p1 int',@p0=N'青州',@p1=8

三、更新

  更新的方式与Insert类似,不过好点过Insert了,还不错。

  存储过程:

CREATE PROC UpdateCountry
@CountryName nvarchar(50),
@CountryId int OUTPUT
AS
UPDATE Country SET CountryName = @CountryName WHERE CountryId = @CountryId;

  映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.CountryModel,Model" table="Country">
<id name="CountryId" column="CountryId" type="Int32">
<generator class="increment"/>
</id>
<property name="CountryName" column="CountryName" type="String"/>
<!-- 一个Country里面有多个Person -->
<set name="ListPerson" table="Person" generic="true">
<key column="CountryId" foreign-key="FK_Person_Country"/>
<one-to-many class="Model.PersonModel,Model"/>
</set>
<sql-insert>InsertCountry ?,?</sql-insert>
<sql-update>UpdateCountry ?,?</sql-update>
</class>
</hibernate-mapping>

  执行代码如下:

        static void Main(string[] args)
{
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory(); using (ISession session = sessionFactory.OpenSession())
{
CountryModel c = session.Get<CountryModel>();
c.CountryName = "修改测试";
session.Update(c);
session.Flush();
} Console.ReadKey();
}

  监控到SQL执行的语句如下:

exec sp_executesql N'UpdateCountry @p0,@p1',N'@p0 nvarchar(4000),@p1 int',@p0=N'修改测试',@p1=4

四、查询

  首先,创建一个存储过程如下:

CREATE PROC EntityCountry
@CountryId int
AS
SELECT * FROM Country WHERE CountryId =@CountryId

  在NHibernate中,通过命名查询来映射存储过程,使用return返回具体的实体类,使用<return-property>告诉NHibernate使用哪些属性值,允许我们来选择如何引用字段以及属性。

  映射文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.CountryModel,Model" table="Country">
<id name="CountryId" column="CountryId" type="Int32">
<generator class="increment"/>
</id>
<property name="CountryName" column="CountryName" type="String"/>
<!-- 一个Country里面有多个Person -->
<set name="ListPerson" table="Person" generic="true">
<key column="CountryId" foreign-key="FK_Person_Country"/>
<one-to-many class="Model.PersonModel,Model"/>
</set>
</class>
<sql-query name="Cc">
<return class="Model.CountryModel,Model" />
EXEC EntityCountry :CountryId
</sql-query>

</hibernate-mapping>

  执行操作:

        static void Main(string[] args)
{
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory(); using (ISession session = sessionFactory.OpenSession())
{
CountryModel c = session.GetNamedQuery("Cc").SetInt32("CountryId", ).UniqueResult<CountryModel>();
Console.WriteLine(c.CountryName);
} Console.ReadKey();
}

  监控到SQL Server执行的语句如下:

exec sp_executesql N'EXEC EntityCountry @p0',N'@p0 int',@p0=1

  考虑到在NHibernate中使用存储过程并不是一个很好的选择,也无谓学习得太深入了,这个真心不想用。

五、总结

  <sql-query>配置在<class>后面。

  其他增删改要遵循以下顺序:

  •   <sql-insert>
  •   <sql-update>
  •   <sql-delete>

  OK,NHibernate执行存储过程就这么多了,以后用到的时候,再更新。

NHibernate 存储过程 第十四篇的更多相关文章

  1. 解剖SQLSERVER 第十四篇 Vardecimals 存储格式揭秘(译)

    解剖SQLSERVER 第十四篇    Vardecimals 存储格式揭秘(译) http://improve.dk/how-are-vardecimals-stored/ 在这篇文章,我将深入研究 ...

  2. 第十四篇 Integration Services:项目转换

    本篇文章是Integration Services系列的第十四篇,详细内容请参考原文. 简介在前一篇,我们查看了SSIS变量,变量配置和表达式管理动态值.在这一篇,我们使用SQL Server数据商业 ...

  3. Python之路【第十四篇】:AngularJS --暂无内容-待更新

    Python之路[第十四篇]:AngularJS --暂无内容-待更新

  4. 【译】第十四篇 Integration Services:项目转换

    本篇文章是Integration Services系列的第十四篇,详细内容请参考原文. 简介在前一篇,我们查看了SSIS变量,变量配置和表达式管理动态值.在这一篇,我们使用SQL Server数据商业 ...

  5. 跟我学SpringCloud | 第十四篇:Spring Cloud Gateway高级应用

    SpringCloud系列教程 | 第十四篇:Spring Cloud Gateway高级应用 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 ...

  6. SpringBoot第二十四篇:应用监控之Admin

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前一章(S ...

  7. Egret入门学习日记 --- 第十四篇(书中 5.4~5.6节 内容)

    第十四篇(书中 5.4~5.6节 内容) 书中内容: 总结 5.4节 内容重点: 1.如何编写自定义组件? 跟着做: 重点1:如何编写自定义组件? 文中提到了重要的两点. 好,我们来试试看. 第一步, ...

  8. Spring Cloud第十四篇 | Api网关Zuul

    ​ 本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...

  9. Hibernate(十四篇)

    (一)Hibernate简介 (二)hibernate配置管理 (三)Hibernate对象-关系映射文件 (四)Hibernate API详解 (五)Hibernate一级缓存 (六)Hiberna ...

随机推荐

  1. python基础===继承

    编写类时,并非总是要从空白开始.如果你要编写的类是另一个现成类的特殊版本,可使用继承.一个类继承另一个类时,它将自动获得另一个类的所有属性和方法:原有的类称为父类,而新类称为子类.子类继承了其父类的所 ...

  2. 刷新SqlServer数据库中所有的视图

    ALTER PROCEDURE sp_refallview AS --刷新所有视图 DECLARE @ViewName VARCHAR(MAX); DECLARE @i INT; ; DECLARE ...

  3. VS2017MVC+EF+MySQL环境搭建

    记录一次环境搭建的过程以及出现的问题和解决方法. 编译器Visual Studio 2017Enterprise Edition 1.新建一个MVC应用程序2.在新建的MVC程序中选择Models - ...

  4. Hadoop-MR[会用]MR程序的运行模式

    1.简介 现在很少用到使用MR计算框架来实现功能,通常的做法是使用hive等工具辅助完成.但是对于其底层MR的原理还是有必要做一些了解. 2.MR客户端程序实现套路 这一小节总结归纳编写mr客户端程序 ...

  5. 4.Python3标准库--算法

    (一)functools:管理函数的工具 import functools ''' functools模块提供了一些工具来管理或扩展和其他callable对象,从而不必完全重写 ''' 1.修饰符 f ...

  6. APP运营

    产品相关术语 APP:application的简写,即应用. 开发商:也叫CP,即ContentProvider内容提供商. 发行商(运营商):代理CP开发出来的产品. 联运:CP和渠道联合运营产品. ...

  7. mysql 服务器配置

    Windows: 1.在bin目录下执行mysqld.exe --install-manual安装服务(删除命令是mysqld.exe --remove) 2.执行net start mysql启动服 ...

  8. Java之CyclicBarrier使用

    http://blog.csdn.net/shihuacai/article/details/8856407 1.类说明: 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (commo ...

  9. Django 1.6在Windows平台下的配置

    Django 1.6 在Windows平台下的配置 前言 最近两天研究了下Django1.6在Windows平台中的配置安装,服务器采用Apache.期间遇到过许多新手所遇到的各种问题,也算是一种宝贵 ...

  10. AC日记——送花 洛谷 P2073

    送花 思路: 线段树: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct TreeN ...