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 ...
随机推荐
- 插入三层treeview代码
#region treetView加载 private void treeViewLoad() { DataView dv = navds.tbSiteKind.AsDataView(); treeV ...
- 【转】移动端App测试实用指南
转自:互联网那点事 英文原文: http://mobile.smashingmagazine.com/2012/10/22/a-guide-to-mobile-app-testing/ 测试人员常被看 ...
- iterm2相关配置
使用 iterm2 登陆 公司堡垒机 进行 上传 下载文件 等维护操作.. 1.需要安装iterm2 软件 http://iterm2.com/ 下载安装 2.安装brew ruby -e &quo ...
- 关于MVC中DropDownListFor的一个bug
如以下代码: //后台 代码 ViewData["source_type"] = new List<SelectListItem> { "}, "} ...
- 【模式识别】Learning To Rank之RankBoost
RankBoost的思想比較简单,是二元Learning to rank的常规思路:通过构造目标分类器,使得pair之间的对象存在相对大小关系.通俗点说,把对象组成一对对的pair,比方一组排序r1& ...
- android144 360 快捷方式
package com.example; import android.net.Uri; import android.os.Bundle; import android.app.Activity; ...
- objective-c对NSArray的学习
转自:http://gekie.iteye.com/blog/1086256 NSARRAY简单的使用 定义数组,遍历数组: 1 2 3 4 5 6 7 8 NSArray *array; array ...
- SQL Server压缩日志及数据库文件大小
请按步骤进行,未进行前面的步骤时,请不要做后面的步骤,以免损坏你的数据库. 一般不建议做第4,6两步,第4步不安全,有可能损坏数据库或丢失数据.第6步如果日志达到上限,则以后的数据库处理会失败,在清理 ...
- Linux下php安装phpredis
说明:php安装目录:/usr/local/php5php.ini配置文件路径:/usr/local/php5/etc/php.iniNginx安装目录:/usr/local/nginxNginx网站 ...
- 用Linux安装光盘修复GRUB
转载:http://lgn21st.iteye.com/blog/179455 需要开视频会议,我不得零时从Ubuntu切换回去百年难道用一次的WinXP...发现自己的XP系统很混乱...决定重新装 ...