1. Many to one and One to Many
class Parent
{
1.首先根据Code Smith自动生成的属性
2. 手动添加
private IList<Child> childs
public IList<Child> Childs
{
get {
if (childs == null)
childs = new List<Child> ();
return childs;
}
set { childs = value; }
}
}
Parent.hbm.xml例子(Build Action设置为Embedded Resource)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Model.Parent,Test.Model" table="Person" lazy="false">
<id name="Id" column="id" type="Int32">
<generator class="identity" />
</id>
<property name="Name" column="name" type="String" length="50" />
<bag name="Childs" table="Child" cascade="all" inverse="true">
<key column="id" />
<one-to-many class="Test.Model.Child, Test.Model" />
</bag>
</class>
</hibernate-mapping>
class Child
{
1. 首先根据Code Smith自动生成的属性
2.手动删除自动生成的private int _ParentID;以及属性
否则出现:NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’错误。
3.手动添加
private Parent_Parent;
public Person Parent
{
get
{
if (_Parent == null) _Parent = new Parent();
return _Parent;
}
set
{
_Parent = value;
}
}
}
Child.hbm.xml例子
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.Model.Child,Test.Model" table="Child" lazy="false">
<id name="ChildID" column="ChildID" type="Int32">
<generator class="identity" />
</id>
<property name="Name" column="name" type="String" length="50" />
<many-to-one name="Parent" column="PersonID" class="Test.Model.Parent, Test.Model"/>
</class>
</hibernate-mapping>
2. App config例子
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=localhost;Initial Catalog=NHibernate;Integrated Security=SSPI</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="default_schema">NHibernate.dbo</property>
<property name="show_sql">true</property>
<!-- HBM Mapping Files -->
<mapping assembly="Test.Model" />
</session-factory>
</hibernate-configuration>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
3. NUnit 测试代码
[TestMethod]
public void TestMethod1()
{
Configuration cf = new Configuration().AddAssembly("Test.Model"); ;
ISessionFactory sf = cf.BuildSessionFactory();
ISession s = sf.OpenSession();
Parent p = new Parent();
p.Name = "parent";
Child child = new Child();
child.Name = "test child";
child.Parent = p; // 必须设置父对象才能正确保存!
p.Childs.Add(child);
ITransaction trans = s.BeginTransaction();
try
{
s.Save(p);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
}
}