Nhibernate3.3.3sp1基础搭建测试

实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NHibernateTest.Entity
{public class Customer{public virtual int CustomerID { get; set; }public virtual string Version { get; set; }public virtual string FirstName { get; set; }public virtual string LastName { get; set; }}
}

View Code

映射XML(嵌入的资源)

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"assembly="NHibernateTest" namespace="NHibernateTest.Entity"><class name=" NHibernateTest.Entity.Customer,NHibernateTest" table="Customer" lazy="false"><id name="CustomerID" column="CustomerID" type="int"><generator class="native" /></id><property name="Version" column="Version" type="String" length="50"/><property name="FirstName" column="FirstName" type="String" length="50"/><property name="LastName" column="LastName" type="String" length="50" /></class>
</hibernate-mapping>

View Code

实体调用测试

using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
//using NHibernate.Expression;//nh低版本才有该引用,nh2.1以上则引用using NHibernate.Criterion;
using NHibernateTest.Entity;
using NHibernate.Criterion;
namespace NHibernateTest
{/// <summary>/// CustomerFixture 的摘要说明。/// </summary>public class CustomerFixture{public CustomerFixture(){//// TODO: 在此处添加构造函数逻辑//
        }public void ValidateQuickStart(){try{//得到NHibernate的配置//MyConfiguration config = new MyConfiguration();//Configuration cfg = config.GetConfig();NHibernateHelper nhh = new NHibernateHelper();//ISessionFactory factory = cfg.BuildSessionFactory();//ISession session = factory.OpenSession();ISession session = nhh.GetSession();ITransaction transaction = session.BeginTransaction();//ISessionFactory factory = Configuration.BuildSessionFactory();
Customer newCustomer = null;try{newCustomer = (Customer)session.Load(typeof(Customer), 2);}catch{}if (newCustomer == null){newCustomer = new Customer();newCustomer.FirstName = "Joseph Cool";newCustomer.LastName = "joe@cool.com";newCustomer.Version = DateTime.Now.ToString();// Tell NHibernate that this object should be saved
                    session.Save(newCustomer);}// commit all of the changes to the DB and close the ISession
                transaction.Commit();session.Close();///首先,我们要从ISessionFactory中获取一个ISession(NHibernate的工作单元)。///ISessionFactory可以创建并打开新的Session。///一个Session代表一个单线程的单元操作。 ///ISessionFactory是线程安全的,很多线程可以同时访问它。///ISession不是线程安全的,它代表与数据库之间的一次操作。///ISession通过ISessionFactory打开,在所有的工作完成后,需要关闭。 ///ISessionFactory通常是个线程安全的全局对象,只需要被实例化一次。///我们可以使用GoF23中的单例(Singleton)模式在程序中创建ISessionFactory。///这个实例我编写了一个辅助类NHibernateHelper 用于创建ISessionFactory并配置ISessionFactory和打开///一个新的Session单线程的方法,之后在每个数据操作类可以使用这个辅助类创建ISession 。// open another session to retrieve the just inserted Customer//session = factory.OpenSession();
session = nhh.GetSession();Customer joeCool = (Customer)session.Load(typeof(Customer), 2);// set Joe Cool's Last Login propertyjoeCool.Version = DateTime.Now.ToString();// flush the changes from the Session to the Database
                session.Flush();IList recentCustomers = session.CreateCriteria(typeof(Customer)).Add(Expression.Gt("Version", new DateTime(2004, 03, 14, 20, 0, 0).ToString())).List();foreach (Customer Customer in recentCustomers){//Assert.IsTrue(Customer.LastLogon > (new DateTime(2004, 03, 14, 20, 0, 0)) );
                    Console.WriteLine(Customer.FirstName);Console.WriteLine(Customer.LastName);}session.Close();}catch (Exception ex){Console.WriteLine(ex.Message);Console.WriteLine(ex.StackTrace);}Console.ReadLine();}}
}

View Code

配置文件(始终复制到目录)

<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" ><session-factory name="NHibernateTest"><property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property><property name="connection.connection_string">Server=(local);initial catalog=NHTest;Integrated Security=SSPI</property><property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property><mapping assembly="NHibernateTest"/></session-factory>
</hibernate-configuration>

View Code

Session工厂

using NHibernate;
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NHibernateTest
{public class NHibernateHelper{private ISessionFactory _sessionFactory;public NHibernateHelper(){_sessionFactory = GetSessionFactory();}private ISessionFactory GetSessionFactory(){return (new Configuration()).Configure().BuildSessionFactory();//return (new Configuration()).Configure("D:\develop\Codes\C#\SpringTest\Spring\NHibernateTest\hibernate.cfg.xml").BuildSessionFactory();
        }public ISession GetSession(){return _sessionFactory.OpenSession();}}
}

View Code

sql(需建NHTest库)

    create Table Customer(CustomerID int primary key identity(1,1) not null,[Version] varchar(50) not null,FirstName varchar(50) not null,LastName varchar(50) not null)create Table [Order](OrderID int primary key identity(1,1) not null,[Version] varchar(50) not null,OrderDate date not null,CustomerID int not null foreign key references [Customer](CustomerID))create Table Product(ProductID int Primary key identity(1,1) not null,[Version] varchar(50),Name varchar(50),Cost varchar(50))create Table OrderProduct(OrderID int not null foreign key references [Order](OrderID),ProductID int not null foreign key references [Product](ProductID))insert into Customer([Version],FirstName,LastName) values('1.0', 'sam', 'sir')
insert into [Order]([Version],OrderDate,CustomerID) values('1.0',GETDATE(),2)
insert into Product([Version],Name,Cost) values('1.0','黑莓','$30')
insert into OrderProduct values(2,3)

View Code

posted on 2013-11-06 00:53 FlowingSun 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/FlowingSun/p/3409666.html

Nhibernate3.3.3sp1基础搭建测试相关推荐

  1. 111-STM32+Air724UG基本控制篇(自建物联网平台)-基础搭建测试-Android扫码绑定Air724,并通过MQTT和模组实现远程通信控制

    说明 前面章节已经搭建好了可以测试Android和设备之间实现通信的服务器. 这节把整体运行测试里面的Android和单片机程序里面的MQTT信息改为自己的服务器 然后测试下通信. 修改单片机程序 1 ...

  2. 最新最全vuepress零基础搭建(github搭建+新增插件)

    最新最全vuepress零基础搭建 标注:最终版以及修改最终都在www.javanode.cn是最终版本,在学习中需要修改的内容以及笔记全在这个网站,谢谢!有任何不妥的地方望纠正 看完了,发现对你有用 ...

  3. 搭建测试环境_当面试时被问到“搭建过测试环境吗”, 身为小白要怎么回答?...

    导语:很多人在面试软件测试的过程中,经常被问到"你会搭建测试环境吗"面对这样的提问,你知道怎么回答么?>>>> 怎 么 回 答 面试的时突然被问到,很多人的 ...

  4. 搭建测试环境、面向对象

    1.搭建测试环境 import os,sysBASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.pat ...

  5. 零基础搭建基于知识图谱的电影问答系统

    零基础搭建基于知识图谱的电影问答系统 一.项目准备 二.项目数据 三.训练问题分类器 四.准备问答模板 五.搭建webapp 六.问题预处理 一.项目准备 首先需要一款python编译器,本人选用的是 ...

  6. vite+element-plus项目基础搭建

    vite+element-plus项目基础搭建 1.引言 2.为什么是Vite? 3.为什么是Element-plus? 4.项目搭建 5.参考文献 1.引言     其实本来不应该写这种CSDN比较 ...

  7. CMDB开发之基础搭建

    cmdb的介绍与需求 CMDB(配置管理数据库)存储与管理企业IT架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧密相联,支持这些流程的运转.发挥配置信息的价值,同时依赖于相关流程保证数据 ...

  8. Jmeter性能测试【应用场景、性能测试流程、搭建测试环境】

    目录 一.性能测试的概念 二.性能测试类型 三.性能测试应用场景(领域) 四.性能测试常用的指标 五.性能测试流程 六.搭建测试环境 七.测试用例设计和脚本开发 八.测试数据准备 九.性能测试执行和管 ...

  9. DCache搭建测试

    DCache搭建测试 工作需要,提前熟悉下部署过程,如有错误还望指点. 简介 背景 Dcache使用腾讯Tars框架开发,属于分布式的NoSQL存储系统.数据存储在内存中,还可以连接后端DB做数据的持 ...

  10. 黑马头条项目 一 项目设计及基础搭建

    黑马头条项目之项目设计及基础搭建 一.概述 工程基于Spring-boot 2.1.5.RELEASE 版本构建,工程父项目为heima-leadnews,并通过继承方式集成Spring-boot. ...

最新文章

  1. linux内核如何安装vim,如何在Linux 中安装和使用 PacVim?
  2. try/ catch/ finally, 你不知道的细节,很骚!
  3. 将 a.txt 文件中的单词与 b.txt 文件中的单词交替合并到 c.txt 文件 中
  4. 二分查找(划分时左右元素个数不相等)解析+代码
  5. akka2.5_发布Akka Toolkit 2.3
  6. 如何用texstudio下载ctex_公众号素材库视频如何下载,用这种方法就可以哦
  7. 2022年上半年系统集成项目管理工程师下午真题及答案解析
  8. 阿里面试算法题(一)
  9. SSM期末复习题(仅供参考)
  10. 中级微观经济学笔记整理
  11. ng4 html路由跳转,Angular4.x通过路由守卫实现动态跳转界面步骤详解
  12. Fixture证书权限导致CSR申请证书失败的问题 [已解决]
  13. vue-element-admin 框架结构粗解
  14. Redis设置过期时间为当月月底-----自动计算
  15. STM32 USB使用记录:使用CDC类虚拟串口(VCP)进行通讯
  16. android 反编译解析.
  17. Java故事之路在脚下
  18. IAR修改工程名方法
  19. Dapp 投票 Voting 实现流程
  20. 数据库恢复技术-数据库习题

热门文章

  1. 解决办法:ubuntu登录后,桌面空空如也,状态栏没了
  2. 管理感悟:减少代码量的好办法
  3. 管理感悟:不要告诉主管只有一条路
  4. Java铬钼钢车架几何_车架的几何尺寸
  5. 一年级学python_Python这个黑科技,后悔没有早点学起来
  6. Linux打开关闭ping
  7. 最近win7更新后出现第二次打开IDE(delphi2007)的时候提示无法打开EditorLineEnds.ttr这个文件...
  8. window.onscroll页面滚动条滚动事件
  9. 连发12款软硬件产品 瑞星领跑企业级安全市场
  10. StructLayout(LayoutKind.Sequential)(转)