NHibernate 学习总结(Many to One, App Config and NUnit)
2009-05-21 15:03
519 查看
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();
}
}
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();
}
}
相关文章推荐
- django 学习个人总结 之many_to_one
- fastSpring one-to-many 学习日记
- MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)
- 我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)
- 码农小汪-Hibernate学习4-关联关系映射many-to-one,many-to-one,one-to-many
- Nhibernate学习起步之many-to-one篇
- 用MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)
- Nhibernate中 Many-To-One 中lazy="proxy" 延迟不起作用的原因
- NHibernate的关联映射(one-to-one,one-to-many,many-to-many)以及cascade分析
- NHibernate的关联映射(one-to-one,one-to-many,many-to-many)以及cascade分析
- hibernate学习之——one-to-many(双向一对多关系)
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习起步之many-to-one篇
- Hibernate学习总结:OneToOne单向主键关联
- nhibernate: one-to-many映射
- Entity Framework 实体关系总结:one-to-one, one-to-many, many-to-many
- hibernate学习4之one-to-many单向关联(不推荐)
- NHibernate学习笔记(二):one-to-one关系映射
- Nhibernate学习起步之many-to-one篇
- 码农小汪-Hibernate学习8-hibernate关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable