花了大概将近三个小时,在配置文件间来回的折腾,终于人品爆发,老天让我感动了,终于成功了,先将我的遇到的种种挫折和弯路给大家说说,让你们少走弯路【我太阳,NHibernate比Hibernate麻烦多了,还是MyEclipse配起来简单,当初我第一次配Hibernate花了十分钟,汗~】,废话不多说,放代码
首先,数据库
USE Test
CREATE TABLE [dbo].[Cat](
[CatId] [ char](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ Name] [ nvarchar](16) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Sex] [ nchar](1) COLLATE Chinese_PRC_CI_AS NULL,
[Weight] [ real] NULL,
CONSTRAINT [PK_Cat] PRIMARY KEY CLUSTERED
(
[CatId] ASC
) WITH (IGNORE_DUP_KEY = OFF) ON [ PRIMARY]
) ON [ PRIMARY]
GO
CREATE TABLE [dbo].[Cat](
[CatId] [ char](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ Name] [ nvarchar](16) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Sex] [ nchar](1) COLLATE Chinese_PRC_CI_AS NULL,
[Weight] [ real] NULL,
CONSTRAINT [PK_Cat] PRIMARY KEY CLUSTERED
(
[CatId] ASC
) WITH (IGNORE_DUP_KEY = OFF) ON [ PRIMARY]
) ON [ PRIMARY]
GO
我用的NHibernate版本是2.1.2GA 下载地址
http://down.ddvip.com/view/119296618916063.html
首先新建个ClassLibirary工程,取名NHibernate.Domain,然后添加引用
NHibernate.dll,NHibernate.ByteCode.Castle.dll【在Required_For_LazyLoading\Castle目录下】
新建个类Cat.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernate.Domain
{
public
class Cat
{
private
string id;
private
string name;
private
char sex;
private
float weight;
public Cat()
{
}
public virtual string Id
{
get {
return id; }
set { id = value; }
}
public virtual string Name
{
get {
return name; }
set { name = value; }
}
public virtual char Sex
{
get {
return sex; }
set { sex = value; }
}
public virtual float Weight
{
get {
return weight; }
set { weight = value; }
}
}
}
然后添加Cat.cs的映射文件Cat.hbm.xml【Nhibernate数据库配置文件】
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate.Domain" assembly ="NHibernate.Domain" >
< class name ="Cat" table ="Cat" >
<!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
< id name ="Id" >
< column name ="CatId" sql-type ="char(32)" not-null ="true" />
< generator class ="uuid.hex" />
</ id >
<!-- A cat has to have a name, but it shouldn' be too long. -->
< property name ="Name" >
< column name ="Name" length ="16" not-null ="true" />
</ property >
< property name ="Sex" />
< property name ="Weight" />
</ class >
</ hibernate-mapping >
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate.Domain" assembly ="NHibernate.Domain" >
< class name ="Cat" table ="Cat" >
<!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
< id name ="Id" >
< column name ="CatId" sql-type ="char(32)" not-null ="true" />
< generator class ="uuid.hex" />
</ id >
<!-- A cat has to have a name, but it shouldn' be too long. -->
< property name ="Name" >
< column name ="Name" length ="16" not-null ="true" />
</ property >
< property name ="Sex" />
< property name ="Weight" />
</ class >
</ hibernate-mapping >
记得Cat.hbm.xml文件右键属性中的Bulid Action选择Embedded Resource
在上述项目中添加个控制台程序取名Test,添加引用NHibernate.dll,NHibernate.ByteCode.Castle.dll
然后再项目中添加hibernate.cfg.xml文件,代码如下:
<?
xml
version
='1.0'
encoding
='utf-8'
?>
< hibernate-configuration xmlns ="urn:nhibernate-configuration-2.2" >
< session-factory >
< property name ="connection.provider" >NHibernate.Connection.DriverConnectionProvider </ property >
< property name ="connection.driver_class" >NHibernate.Driver.SqlClientDriver </ property >
< property name ="connection.connection_string" >Data Source=VISIONSKY;Initial Catalog=Test;Integrated Security=True </ property >
< property name ="show_sql" >true </ property >
< property name ="dialect" >NHibernate.Dialect.MsSql2005Dialect </ property >
< property name ="use_outer_join" >true </ property >
< property name ="query.substitutions" >true 1, false 0, yes 'Y', no 'N' </ property >
< property name ="proxyfactory.factory_class" >NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </ property >
< mapping assembly ="NHibernate.Domain" />
</ session-factory >
</ hibernate-configuration >
< hibernate-configuration xmlns ="urn:nhibernate-configuration-2.2" >
< session-factory >
< property name ="connection.provider" >NHibernate.Connection.DriverConnectionProvider </ property >
< property name ="connection.driver_class" >NHibernate.Driver.SqlClientDriver </ property >
< property name ="connection.connection_string" >Data Source=VISIONSKY;Initial Catalog=Test;Integrated Security=True </ property >
< property name ="show_sql" >true </ property >
< property name ="dialect" >NHibernate.Dialect.MsSql2005Dialect </ property >
< property name ="use_outer_join" >true </ property >
< property name ="query.substitutions" >true 1, false 0, yes 'Y', no 'N' </ property >
< property name ="proxyfactory.factory_class" >NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </ property >
< mapping assembly ="NHibernate.Domain" />
</ session-factory >
</ hibernate-configuration >
注意:必须保证
xmlns="urn:nhibernate-configuration-2.2" 跟映射文件一致,否则会报错
另外
hibernate.cfg.xml 右键属性copy to output directory这一项选择Copy always
最后在Program.cs文件中添加如下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Domain;
namespace Test
{
class Program
{
static
void Main(
string[] args)
{
ISessionFactory factory =
new Configuration().Configure().BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction tx = session.BeginTransaction();
Cat princess =
new Cat();
princess.Name =
"Princess";
princess.Sex = 'F';
princess.Weight = 7.4f;
session.Save(princess);
tx.Commit();
session.Close();
}
}
}
然后点运行,OK,成功~~~!
转载于:https://blog.51cto.com/visionsky/363760
本文详细记录了使用NHibernate 2.1.2GA版本进行数据库配置的过程,包括创建数据库表、定义实体类及映射文件、配置hibernate.cfg.xml等步骤,并分享了作者在配置过程中的经验和教训。
3872

被折叠的 条评论
为什么被折叠?



