花了大概将近三个小时,在配置文件间来回的折腾,终于人品爆发,老天让我感动了,终于成功了,先将我的遇到的种种挫折和弯路给大家说说,让你们少走弯路【我太阳,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文件





namespace NHibernate.Domain








public Cat()



public virtual string Id





public virtual string Name





public virtual char Sex





public virtual float Weight






然后添加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文件中添加如下代码








namespace Test
























然后点运行,OK,成功~~~!
转载于:https://blog.51cto.com/visionsky/363760