实体类

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; }
}
}

映射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=""/>
<property name="FirstName" column="FirstName" type="String" length=""/>
<property name="LastName" column="LastName" type="String" length="" />
</class>
</hibernate-mapping>

实体调用测试

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), );
}
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), ); // set Joe Cool's Last Login property
joeCool.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(, , , , , ).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();
}
}
}

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

<?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>

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();
}
}
}

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)

Nhibernate3.3.3sp1基础搭建测试的更多相关文章

  1. 大数据基础-2-Hadoop-1环境搭建测试

    Hadoop环境搭建测试 1 安装软件 1.1 规划目录 /opt [root@host2 ~]# cd /opt [root@host2 opt]# mkdir java [root@host2 o ...

  2. Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试

    标签:Linux 域名 Nginx 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xpleaf.blog.51cto.com/9 ...

  3. 【原】Spring整合Shiro基础搭建[3]

    1.前言 上个Shiro Demo基础搭建是基于官方的快速入门版本,没有集成其他框架,只是简单的通过Main方法来执行Shiro工作流程,并测试一下比较核心的函数:但在企业开发中一般都会集成Sprin ...

  4. 02.基于IDEA+Spring+Maven搭建测试项目--详细过程

    一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...

  5. Redis codis 搭建测试

    codis Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别,有部分命令支持 Codis ...

  6. python学习之正则表达式,StringIO模块,异常处理,搭建测试环境

    python正则表达式 引入一个强大的匹配功能来匹配字符串 import re 正则表达式的表示类型raw string类型(原生字符串类型) r'sa\\/sad/asd'用r转为raw strin ...

  7. 0基础搭建Hadoop大数据处理-编程

    Hadoop的编程可以是在Linux环境或Winows环境中,在此以Windows环境为示例,以Eclipse工具为主(也可以用IDEA).网上也有很多开发的文章,在此也参考他们的内容只作简单的介绍和 ...

  8. MFC程序使用GTest搭建测试框架

    一.起源 最近对单元测试比较感兴趣,之后就上网搜了一些测试的框架,C++项目使用的测试框架基本上都使用的GoogleTest,之后就开启了gtest的学习之路. 主要是根据<玩转Google开源 ...

  9. LVS+keepalived快速搭建测试环境

    #LVS+keepalived快速搭建测试环境 #LVS+keepalived快速搭建测试环境 #centos6 X64 # LVS 负载均衡模式:DR(直接路由) 192.168.18.31 mas ...

随机推荐

  1. 时间的函数,sleep,clock,gettickcount,QueryPerformanceCounter(转)

    介绍 我 们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都 会用到时间函数.还比如我们通过记录函数或者算 ...

  2. sql2008“备份集中的数据库备份与现有数据库不同”解决方法

    因为是在另一台电脑对同名数据库做的备份,用常规方法还原,提示不是相同数据库,不让还原,在网上找到下面的方法解决了: 一.右击系统数据库master,新建查询 执行以下SQL代码: RESTORE DA ...

  3. python的深拷贝和浅拷贝

    import copy list1=[1,2,3,4,5] c_list1=list1 c_list1[0]=-1 for i in list1: print str(i)+' ', #输出的世 -1 ...

  4. linux 一些遇到的问题

    亚马逊云服务器上的YUM源有些问题,是64位系统,安装GCC的时候要用 yum install gcc44 yum install gcc44-c++ 查看源的列表和禁用(开启)源 yum-confi ...

  5. 超级终端和SecureCRT进行Console口的连接

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  6. windows 下实现函数打桩:拦截API方式

    windows 下实现函数打桩:拦截API方式            近期由于工作须要,開始研究函数打桩的方法. 由于不想对project做过多的改动,于是放弃了使用Google gmock的想法. ...

  7. zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap

    Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  8. Struts2之异常处理

    一.学习案例:通过在input.jsp页面输入登录账号和password測试异常处理机制. 二.案例分析:struts2提供了局部异常处理机制和全局异常处理机制.局部优先于全局异常处理,当异常找不到局 ...

  9. sync_binlog innodb_flush_log_at_trx_commit 浅析 传

    http://blog.itpub.net/22664653/viewspace-1063134/

  10. Tomcat以指定JDK运行

    如果一台机器上有多个Tomcat,可能存在不同的Tomcat需要不同版本JDK才能运行的情况,这时候就需要指定JDK来同时运行多个Tomcat了. 在windows环境下以批处理文件方式启动tomca ...