使用NHibernate 注意要点
1. 应用层的工程的App.config也要有NHibernate相关的配置。
2. 数据库设计当中,表名,字段名不能有数据库的关键字,如:user, group.....字段名最好不与表名相同
3.实体映射文件(*.hbm.xml)要设置为Embeded Resource(嵌入式资源)文件。
4.对于one-to-many, 配置要用set, 实体类要用Iesi.Collections.ISet。
例如:
Role.hbm.xml
5.实体类一定要override Equals方法与GetHashcode方法。
6.对数据进行插入、修改、删除要使用事务:
ITransaction tran=session.BegionTransaction();
//Do something
tran.Commit();
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add key="hibernate.show_sql" value="false"/>
<add
key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string" value="Server=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=SSPI"
/>
</nhibernate>
</configuration>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add key="hibernate.show_sql" value="false"/>
<add
key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string" value="Server=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=SSPI"
/>
</nhibernate>
</configuration>
2. 数据库设计当中,表名,字段名不能有数据库的关键字,如:user, group.....字段名最好不与表名相同
3.实体映射文件(*.hbm.xml)要设置为Embeded Resource(嵌入式资源)文件。
4.对于one-to-many, 配置要用set, 实体类要用Iesi.Collections.ISet。
例如:
Role.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Common.Entity.Role,Common" table="Role">
<id name="RoleKey" type="Int32">
<generator class="assigned" />
</id>
<property name="RoleName" type="String"/>
<set name="UserMembers" inverse="true" cascade="all">
<key column="RoleKey"/>
<one-to-many class="Common.Entity.User,Common"/>
</set>
</class>
</hibernate-mapping>
Role.cs<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Common.Entity.Role,Common" table="Role">
<id name="RoleKey" type="Int32">
<generator class="assigned" />
</id>
<property name="RoleName" type="String"/>
<set name="UserMembers" inverse="true" cascade="all">
<key column="RoleKey"/>
<one-to-many class="Common.Entity.User,Common"/>
</set>
</class>
</hibernate-mapping>
public class Role
{
private int roleKey;
private string roleName;
private ISet userMembers;
public string RoleName
{
get
{
return roleName;
}
set
{
roleName = value;
}
}
public int RoleKey
{
get
{
return roleKey;
}
set
{
roleKey = value;
}
}
public ISet UserMembers
{
get
{
if (userMembers == null)
{
userMembers = new HashedSet();
}
return userMembers;
}
set
{
userMembers = value;
}
}
}
{
private int roleKey;
private string roleName;
private ISet userMembers;
public string RoleName
{
get
{
return roleName;
}
set
{
roleName = value;
}
}
public int RoleKey
{
get
{
return roleKey;
}
set
{
roleKey = value;
}
}
public ISet UserMembers
{
get
{
if (userMembers == null)
{
userMembers = new HashedSet();
}
return userMembers;
}
set
{
userMembers = value;
}
}
}
5.实体类一定要override Equals方法与GetHashcode方法。
6.对数据进行插入、修改、删除要使用事务:
ITransaction tran=session.BegionTransaction();
//Do something
tran.Commit();