Nhibernate学习起步之many-to-one篇(转)
2007-03-29 16:30
387 查看
1. 学习目的:
通过进一步学习nhibernate基础知识,在实现单表CRUD的基础上,实现两表之间one-to-many的关系.
2. 开发环境+必要准备
开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
必要准备: 学习上篇文章单表操作
3. 对上篇文章中部分解释
1)在User.hbm.xml中class节点中有一个lazy的属性,这个属性用于指定是否需要延迟加载(lazy loading),在官方文档中称为:lazy fecting.可以说延迟加载是nhibernate最好的特点,因为它可以在父类中透明的加载子类集合,这对于many-to-one的业务逻辑中,真是方便极了。但是有些时候,父类是不需要携带子类信息的。这时候如果也加载,无疑对性能是一种损失。在映射文件的class节点中可以通过配置lazy属性来指定是否支持延迟加载,这就更灵活多了。
2) 在User.hbm.xml中generate节点,代表的是主键的生成方式,上个例子中的”native”根据底层数据库的能力选择identity,hilo,sequence中的一个,比如在MS Sql中,使我们最经常使用的自动增长字段,每次加1.
3) 在NHibernateHelper.cs中,创建Configuration对象的代码:new Configuration().Configure(@"E:\myproject\nhibernatestudy\simle1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml");因为我是在单元测试中调试,所以将绝对路径的配置文件传递给构造函数。如果在windows app或者web app可以不用传递该参数。
4. 实现步骤
1)确定实现的业务需求:用户工资管理系统
2) 打开上篇文章中的NHibernateStudy1解决方案。向项目NhibernateSample1添加类Salary;代码如下
using System;
using System.Collections.Generic;
using System.Text;
namespace NhibernateSample1
3) 更改User.cs,在User里面添加SalaryList属性:
private System.Collections.IList _salaryList;
2 public System.Collections.IList SalaryList
6<bag name="SalaryList" table="Salary" inverse="true" lazy="true" cascade="all">
<key column="Id"/>
<one-to-many class="NhibernateSample1.Salary,NhibernateSample1"></one-to-many>
</bag>
5)编写类Salary的映射文件:Salary.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NhibernateSample1.Salary,NhibernateSample1" table="Salary" lazy="false">
<id name="Id" column="Id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Year" column="Year" type="Int32" not-null="true"></property>
<property name="Month" column="Month" type="Int32" not-null="true"></property>
<property name="Envy" column="Envy" type="Int32" not-null="true"></property>
<property name="Money" column="Money" type="Decimal" not-null="true"></property>
<many-to-one name="Employee" column="Uid" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
6)编写CRUD类
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace NhibernateSample1
7) 编写单元测试类:UnitTest1.cs
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NhibernateSample1;
namespace TestProject1
加载测试元数据,直到Test()通过。
总结:通过进一步学习nhiberate,发现ORM框架真是非常强大。今天先到这里。明天继续。
项目文件:/Files/jillzhang/simple2.rar
通过进一步学习nhibernate基础知识,在实现单表CRUD的基础上,实现两表之间one-to-many的关系.
2. 开发环境+必要准备
开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
必要准备: 学习上篇文章单表操作
3. 对上篇文章中部分解释
1)在User.hbm.xml中class节点中有一个lazy的属性,这个属性用于指定是否需要延迟加载(lazy loading),在官方文档中称为:lazy fecting.可以说延迟加载是nhibernate最好的特点,因为它可以在父类中透明的加载子类集合,这对于many-to-one的业务逻辑中,真是方便极了。但是有些时候,父类是不需要携带子类信息的。这时候如果也加载,无疑对性能是一种损失。在映射文件的class节点中可以通过配置lazy属性来指定是否支持延迟加载,这就更灵活多了。
2) 在User.hbm.xml中generate节点,代表的是主键的生成方式,上个例子中的”native”根据底层数据库的能力选择identity,hilo,sequence中的一个,比如在MS Sql中,使我们最经常使用的自动增长字段,每次加1.
3) 在NHibernateHelper.cs中,创建Configuration对象的代码:new Configuration().Configure(@"E:\myproject\nhibernatestudy\simle1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml");因为我是在单元测试中调试,所以将绝对路径的配置文件传递给构造函数。如果在windows app或者web app可以不用传递该参数。
4. 实现步骤
1)确定实现的业务需求:用户工资管理系统
2) 打开上篇文章中的NHibernateStudy1解决方案。向项目NhibernateSample1添加类Salary;代码如下
using System;
using System.Collections.Generic;
using System.Text;
namespace NhibernateSample1
3) 更改User.cs,在User里面添加SalaryList属性:
private System.Collections.IList _salaryList;
2 public System.Collections.IList SalaryList
6<bag name="SalaryList" table="Salary" inverse="true" lazy="true" cascade="all">
<key column="Id"/>
<one-to-many class="NhibernateSample1.Salary,NhibernateSample1"></one-to-many>
</bag>
5)编写类Salary的映射文件:Salary.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NhibernateSample1.Salary,NhibernateSample1" table="Salary" lazy="false">
<id name="Id" column="Id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Year" column="Year" type="Int32" not-null="true"></property>
<property name="Month" column="Month" type="Int32" not-null="true"></property>
<property name="Envy" column="Envy" type="Int32" not-null="true"></property>
<property name="Money" column="Money" type="Decimal" not-null="true"></property>
<many-to-one name="Employee" column="Uid" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
6)编写CRUD类
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace NhibernateSample1
7) 编写单元测试类:UnitTest1.cs
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NhibernateSample1;
namespace TestProject1
加载测试元数据,直到Test()通过。
总结:通过进一步学习nhiberate,发现ORM框架真是非常强大。今天先到这里。明天继续。
项目文件:/Files/jillzhang/simple2.rar
相关文章推荐
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习起步之many-to-one篇(转 明了篇)
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习起步之many-to-one篇
- Nhibernate学习之many-to-many篇
- Nhibernate学习之many-to-many篇
- Nhibernate学习之many-to-many篇
- Nhibernate学习之many-to-many篇
- Nhibernate学习之many-to-many篇(转 明了篇)
- Nhibernate学习之many-to-many篇
- Nhibernate学习之many-to-many篇
- Nhibernate学习之many-to-many篇
- Nhibernate学习之many-to-many篇
- NHibernate 学习总结(Many to One, App Config and NUnit)
- [Nhibernate] one-to-many
- hibernate学习5之one-to-many双向关联