在App.Config文件中
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:hibernate-configuration-2.2">
<!-- an ISessionFactory instance -->
<session-factory xmlns="urn:nhibernate-configuration-2.2">
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.JetDriver.JetDriver, NHibernate.JetDriver</property>
<property name="dialect">NHibernate.JetDriver.JetDialect, NHibernate.JetDriver</property>
<property name="connection.connection_string">Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\appTemp\TestCastle\Castle3\Test.mdb</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="use_proxy_validator">false</property>
</session-factory>
</hibernate-configuration>
</configuration>
注意:
1、xmlns = "urn:nhibernate-configuration-2.2"必须加上。值urn:nhibernate-configuration-2.2和urn:hibernate-configuration-2.2效果一样。
2、use-proxy-validtor设置为false的话,POJO类的属性就不需要标记为virtual。
用NHibernate.Mapping.Attributes配置的简单Pojo类,可以省掉一大堆的XML 。本人喜欢Attribute配置。
[Class(Table = "Users")]
public class UserPojo
{
private int userID;
private string name;
private int age;
private DateTime birthDate;
private string description;
public UserPojo()
{
userID = default(int);
name = default(string);
age = default(int);
birthDate = default(DateTime);
description = default(string);
}
[Id(0, Column = "UserID", TypeType = typeof(int))]
[Generator(1, Class = "identity")]
public int UserID
{
get { return userID; }
set { userID = value; }
}
[Property(Column = "Age", Type = "Int32")]
public int Age
{
get { return age; }
set { age = value; }
}
[Property(Column = "Name", Type = "string")]
public string Name
{
get { return name; }
set { name = value; }
}
[Property(Column = "BirthDate", Type = "DateTime")]
public DateTime BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
[Property(Column = "Description", Type = "string")]
public string Description
{
get { return description; }
set { description = value; }
}
}
注:以上的实体类有个错误,Id的Attribute一定要指定Name属性!!