NHibernate 学习总结(Many to One, App Config and NUnit)

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();

            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值