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. C++之抽象基类与纯虚函数
  2. 制作碳排放强度的空间可视化_【科研成果】吴传清、宋子逸:长江经济带农业碳排放的时空差异特征分析...
  3. 关于switch-case问题
  4. P2634 [国家集训队]聪聪可可(树形dp)
  5. datagridview 动态插入图片_挑战一张照片制作动态PPT背景
  6. 亚马逊云科技中国线上峰会开幕,发力汽车产业链、少年人工智能等
  7. PyG图神经网络框架--构建信息传递网络(MPN)
  8. 测试工程师python面试常问问题_面试测试工程师一般会问些什么?
  9. 从程序员到架构师——踏上架构旅途 思考从未止步
  10. python 日志输出变量_Python日志输出
  11. 数据结构——约瑟夫环(循环链表C语言版)
  12. python输出欢迎某某某_python中怎么写注释
  13. CenterNet: Keypoint Triplets for Object Detectiontection学习笔记
  14. Python开源Devops定时任务管理系统(含定时调用接口、定时ssh远程执行命令)
  15. 新版jadx-gui导入dex会提示Bad checksum
  16. MAC笔记本破解ZIP压缩的文件密码
  17. 远程桌面用户输入法的配置
  18. 如何用qq远程桌面链接到计算机,如何用qq远程控制电脑_qq怎么远程连接对方的电脑-win7之家...
  19. 信号与系统sa函数求积分_信号与系统
  20. Hexo+GitHubPages搭建个人博客网站

热门文章

  1. 解决办法:undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
  2. 雄伟到惊世骇俗的黄羊山超级相控阵雷达
  3. 管理感悟:能图像不文字
  4. 给LINUX添加一个开机执行脚本
  5. python class是什么_python中什么是类
  6. 集群ddos_《DNS攻击防范科普系列2》 -DNS服务器怎么防DDoS攻击
  7. android卡片式通知,原子通知+超级卡包,OriginOS比传统安卓更懂用户的需求
  8. AD集成DNS区域记录重建及恢复
  9. Android lollipop 更新问题
  10. SQL 修改表字段失败 解决方法