Nhibernate3循序渐进(三): 一对多映射和级联保存

我们知道, 对于数据库中的一对多关系, 我们在建表的时候, 应该在多表这里建立外键

 

我们准备这样一个场景, 大学里的系和学生, 一个系有多个学生

 

Department类和Xml如下:

 

Department.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate3.Domain
{
    public class Department
    {
        public virtual int? ID { getset; }

        public virtual string Name { getset; }
    }
}

 

Department..hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate3.Domain" namespace="NHibernate3.Domain">
  <class name="Department" table="T_Department" lazy="true" >
    <id name="ID" type="int" column="DepartmentID">
      <generator class="native"/>
    </id>
  
    <property name="Name" type="string">
      <column name="Name" length="50"/>
    </property>

  </class>
</hibernate-mapping>

 

 

Student类和Xml如下:

 

Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate3.Domain
{
    public class Student
    {
        public virtual int? ID { getset; }

        public virtual string Name { getset; }

        public virtual Department Dept { getset; }
    }
}

 

 

 

Student.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate3.Domain" namespace="NHibernate3.Domain">
  <class name="Student" table="T_Student" lazy="true" >
    <id name="ID" type="int" column="StudentID">
      <generator class="native"/>
    </id>

    <property name="Name" type="string">
      <column name="Name" length="50"/>
    </property>

    <many-to-one name="Dept" column="DepartmentID" cascade="all"/>

  </class>
</hibernate-mapping>

 

 

保存和查询代码

 

ScSession.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Cfg;
using NHibernate3.Domain;

namespace NHibernate3.DAL
{
    public class ScSession
    {
        private ISessionFactory sessionFactory;

        public ScSession()
        {
            var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
            sessionFactory = cfg.BuildSessionFactory();
        }

        public void Save()
        {
            using (ISession session = this.sessionFactory.OpenSession())
            {
                var dept = new Department { Name = "计算机系" };

                var student1 = new Student { Name = "李四", Dept = dept };
                var student2 = new Student { Name = "张三", Dept = dept };

                ITransaction tran = session.BeginTransaction();
                try
                {
                    //持久化Department类

                    session.Save(student1);
                    session.Save(student2);

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }

        public void GetAll()
        {
            using (ISession session = this.sessionFactory.OpenSession())
            {
                var students = session.CreateQuery(" from Student").List<Student>();

                Console.WriteLine();
                Console.WriteLine("******* 学生信息 *******");
                foreach (Student student in students)
                {
                    Console.WriteLine("学生:{0}", student.Name);

                    Console.WriteLine("系:{0}", student.Dept.Name);
                }
            }
        }
    }
}

 

运行结果:

记录保存成功!

******* 学生信息 *******
学生:李四
系:计算机系
学生:张三
系:计算机系

 

 

 

 

posted @ 2012-06-15 17:40  Master HaKu  阅读(1349)  评论(0编辑  收藏  举报