实体类

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. Hibernate HQL和原生SQL查询的一点区别

    1.createSQLQuery 1.1默认查询的结果为BigDecimal 1.2通过addScalar("CGD_ID", StandardBasicTypes.LONG)可以 ...

  2. Lua学习笔记(四):表和数组

    表 在Lua中,表(table)是十分重要的一种数据结构,实际上Lua对于复杂数据类型也仅提供了表给我们,我们通过表可以实现我们需要的大部分重要的数据结构,比如数组. table类型实现了关联数组,关 ...

  3. C#学习笔记(十三):I/O操作

    C#的IO操作主要是针对文件夹和文件的读取和写入操作,下面我们来学习一下相关操作的类. 获取文件信息 Directory和DirectoryInfo 两个类的功能基本相同,区别如下: 前者继承Syst ...

  4. spring事务注解

    @Transactional只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能. Spring使用声明式事务处理,默认 ...

  5. 如何克隆路由器MAC地址,怎么操作?

    路由器的“MAC地址克隆”的意思是: 不克隆时,从外网访问你的电脑,获得的MAC地址是路由器的mac地址. 克隆后,从外网访问你的电脑,获得的MAC地址是你电脑网卡的mac地址. 实用举例如下: 中国 ...

  6. Autofac实例生命周期

    1.默认,每次请求都会返回一个实例 builder.RegisterType<X>().InstancePerDependency(); 2.Per Lifetime Scope:这个作用 ...

  7. HDU 2136 Largest prime factor 參考代码

    #include <iostream> #include <vector> #include <cmath> using namespace std; const ...

  8. CUDA从入门到精通

    http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通(零):写在前面 在老板的要求下.本博主从2012年上高性能计算课程開始 ...

  9. 【转】Linux下(C/C++)使用system()函数一定要谨慎

    转自:http://my.oschina.net/renhc/blog/53580   曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用 ...

  10. IOS 手绘地图导航

    手绘地图导航 第三方库 NAMapKit, 1)支持在手绘图上标记.缩放 2)支持在单张图片 3)支持瓦片小图片 思路 前提:美工已经切好手绘图,并告知我们当前的缩放级别. 1)确定好手绘图左上角点在 ...