博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nhibernate3.3.3sp1基础搭建测试
阅读量:4599 次
发布时间:2019-06-09

本文共 6558 字,大约阅读时间需要 21 分钟。

实体类

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

映射XML(嵌入的资源)

View Code

实体调用测试

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{    ///     /// CustomerFixture 的摘要说明。    ///     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), 2);                }                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), 2);                // 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(2004, 03, 14, 20, 0, 0).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();        }    }}
View Code

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

NHibernate.Driver.SqlClientDriver
Server=(local);initial catalog=NHTest;Integrated Security=SSPI
NHibernate.Dialect.MsSql2008Dialect
View Code

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

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)
View Code

 

posted on
2013-11-06 00:53 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/FlowingSun/p/3409666.html

你可能感兴趣的文章
深入探究jvm之GC的算法及种类
查看>>
ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决
查看>>
揭秘变量提升
查看>>
Python 变量引用
查看>>
webpack优化 -- happypack
查看>>
webpack优化 -- compression-webpack-plugin 开启gzip
查看>>
无限调用函数add(1)(2)(3)......
查看>>
Centos系统Python环境搭建和项目部署
查看>>
跨区导入
查看>>
.net 异步编程总结
查看>>
正式退役
查看>>
退役后做题记录
查看>>
gdsoi2019题解
查看>>
003_Python3 基本数据类型
查看>>
iis7 应用程序池回收设置
查看>>
UVA 11375 - Matches
查看>>
python识别图中的文字(ocr)
查看>>
教你一眼认出英语单词的意思
查看>>
squid使用NCSA验证
查看>>
黑马程序员----java基础--String字符串
查看>>