88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
NHibernate资源
NHibernate资源现在已经比较多了,但是大部分都是英文了,这里我仅仅挑选几个站点。
NHibernate官方主页:http://www.nhibernate.org/(英文)
NHibernate社区:http://www.nhforge.org/(英文)
NHibernate参考文档2.0.0:http://nhforge.org/doc/nh/en/(英文)
NHibernate之夏系列录像教程:http://www.summerofnhibernate.com/(英文)
88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
一、数据访问层配置:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=."SQLEXPRESS;Initial Catalog=NHibernateSample;
Integrated Security=True;Pooling=False
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<mapping assembly="DomainModel"/>
</session-factory>
</hibernate-configuration>
将复制到输出目录设置为:永远赋值
二、映射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LearnHibernate.Model"
namespace="LearnHibernate.Model">
<class name="LearnHibernate.Model.User, LearnHibernate.Model" table="Users">
<id name="Id" type="Int32" unsaved-value="null">
<column name="id" length="4" sql-type="int" not-null="true" unique="true" index="PK_user"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="name" length="50" sql-type="nvarchar" not-null="false"/>
</property>
<property name="Password" type="String">
<column name="password" length="50" sql-type="nvarchar" not-null="false"/>
</property>
</class>
</hibernate-mapping>
注:将生成操作设置为:嵌入式资源
三、持久化数据
规则
- NHibernate使用属性的getter和setter来实现持久化。
- 属性可设置为public、internal、protected、protected internal或private
注意:
NHibernate默认使用代理功能,要求持久化类不是sealed的,而且其公共方法、属性和事件声明为virtual。在这里,类中的字段要设置为virtual,否则出现“failed: NHibernate.InvalidProxyTypeException : The following types may not be used as proxies: DomainModel.Entities.Customer: method get_CustomerId should be virtual,method set_CustomerId should be virtual”异常。