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; }
}
}
映射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基础搭建测试的更多相关文章
- 大数据基础-2-Hadoop-1环境搭建测试
Hadoop环境搭建测试 1 安装软件 1.1 规划目录 /opt [root@host2 ~]# cd /opt [root@host2 opt]# mkdir java [root@host2 o ...
- Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试
标签:Linux 域名 Nginx 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xpleaf.blog.51cto.com/9 ...
- 【原】Spring整合Shiro基础搭建[3]
1.前言 上个Shiro Demo基础搭建是基于官方的快速入门版本,没有集成其他框架,只是简单的通过Main方法来执行Shiro工作流程,并测试一下比较核心的函数:但在企业开发中一般都会集成Sprin ...
- 02.基于IDEA+Spring+Maven搭建测试项目--详细过程
一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...
- Redis codis 搭建测试
codis Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别,有部分命令支持 Codis ...
- python学习之正则表达式,StringIO模块,异常处理,搭建测试环境
python正则表达式 引入一个强大的匹配功能来匹配字符串 import re 正则表达式的表示类型raw string类型(原生字符串类型) r'sa\\/sad/asd'用r转为raw strin ...
- 0基础搭建Hadoop大数据处理-编程
Hadoop的编程可以是在Linux环境或Winows环境中,在此以Windows环境为示例,以Eclipse工具为主(也可以用IDEA).网上也有很多开发的文章,在此也参考他们的内容只作简单的介绍和 ...
- MFC程序使用GTest搭建测试框架
一.起源 最近对单元测试比较感兴趣,之后就上网搜了一些测试的框架,C++项目使用的测试框架基本上都使用的GoogleTest,之后就开启了gtest的学习之路. 主要是根据<玩转Google开源 ...
- LVS+keepalived快速搭建测试环境
#LVS+keepalived快速搭建测试环境 #LVS+keepalived快速搭建测试环境 #centos6 X64 # LVS 负载均衡模式:DR(直接路由) 192.168.18.31 mas ...
随机推荐
- HDU 1010 Tempter of the Bone (DFS+剪枝)
题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到 ...
- 修改浏览器accept使支持@ResponseBody
原始:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 application/json,text/javascript, ...
- [前端JS学习笔记]JavaScript function
一.函数的声明 1.1 function 命令 function methodName(params) { // code } 如下声明: function test_function(params) ...
- java中的hashcode和euqals的区别和联系
一.equals方法的作用 1.默认情况(没有覆盖equals方法)下equals方法都是调用Object类的equals方法,而Object的equals方法主要用于判断对象的内存地址引用是不是同一 ...
- Python 动态语言
1.在C++中,Animal a = Person(); 这样写是不行的,因为a的内容不能使用Person的内容来填充. 2.在Python中,变量不需要声明,而且可以赋任何值.Python是如何做到 ...
- SQL SERVER NVARCHAR字段INSERT 中文乱码问题解决
INSERT INTO IPatient ( UID, PatientNo, PatientName, PatientGender, Birthday, BloodType, Country, Nat ...
- c# 自己制作一个简单的项目倒计时器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- python选择排序
def select_sort(list): for i in range(len(list)): position = i for j in range(i,len(list)): if list[ ...
- Centos 6.5使用Bumblebee关闭N卡,冷却你的电脑
夏天来了,笔记本装的Centos一直非常热.随着天气的变化,这个问题真的要攻克了.差了下原因可能是双显卡笔记本,N卡驱动不完好,导致风扇狂叫. 昨天安装了nvidia 的显卡驱动本以为时间安静了.但是 ...
- sql server where、group by、order by 执行顺序
2012-02-07 19:39 先where 条件1,再 group by 条件2再 order by 条件3 如果声明了 GROUP BY 子句,输出就分成匹配一个或多个数值的不同组里. 如果出现 ...