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 ...
随机推荐
- 最大流&最小割 - 专题练习
[例1][hdu5889] - 算法结合(BFS+Dinic) 题意 \(N\)个点\(M\)条路径,每条路径长度为\(1\),敌人从\(M\)节点点要进攻\(1\)节点,敌人总是选择最优路径即最短路 ...
- 框架学习笔记:Unity3D的MVC框架——StrangeIoC
作为从AS3页游走过来的人,看见StrangeIoC会额外亲切,因为StrangeIoC的设计和RobotLegs几乎一致,作为一款依赖注入/控制反转(IoC)的MVC框架,StrangeIoC除了使 ...
- Android Layout_Gravity和Gravity
简单来说layout_gravity表示子控件在父容器的位置,gravity表示控件内容在控件内的位置. 上面图片的xml代码 <?xml version="1.0" enc ...
- 【转】使用GDB调试Coredump文件
来自:http://blog.ddup.us/?p=176 写C/C++程序经常要直接和内存打交道,一不小心就会造成程序执行时产生Segment Fault而挂掉.一般这种情况都是因为数组越界访问,空 ...
- Don't Repeat Yourself (不要重复你自己)
DRY是指Don't Repeat Yourself特指在程序设计以及计算中避免重复代码,因为这样会降低灵活性.简洁性,并且可能导致代码之间的矛盾.<The Pragmatic Programm ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- thrift学习笔记
Thrift学习笔记 一:thrift介绍 Thrift是facebook开发的用来处理各不同系统之间数据通讯的rpc服务框架,后来成为apche的开源项目.thrift支持多种程序语言,包括Java ...
- Linux用户及用户组设置
查看当前用户所属的组 groups 查看<user1>, <user2> 和 <user3>所属的组 groups <user1> <user2 ...
- SiteMesh学习笔记
SiteMesh是一个轻量级的web应用框架,实现了Decorator模式.它的目标是将多个页面都将引用的jsp页面通过配置加载到相应的jsp文件中. 在我们的项目中,每个jsp都需要添加两个top和 ...
- enum 在c中的使用
假设一个变量你须要几种可能存在的值,那么就能够被定义成为枚举类型.之所以叫枚举就是说将变量或者叫对象可能存在的情况也能够说是可能的值一一例举出来. 举个样例来说明一吧,为了让大家更明确一点,比方一个 ...