您的位置:首页 > 其它

Nhibernate学习起步之many-to-one篇

2008-02-21 15:16 471 查看
Nhibernate学习起步之many-to-one篇
1. 学习目的:
通过进一步学习nhibernate基础知识,在实现单表CRUD的基础上,实现两表之间one-to-many的关系.

2. 开发环境+必要准备

开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition

必要准备: 学习上篇文章单表操作

3) 更改User.cs,在User里面添加SalaryList属性:
4)修改User.hbm.xml,加入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

private System.Collections.IList _salaryList;
2 public System.Collections.IList SalaryList
6using System;
using System.Collections.Generic;
using System.Text;

namespace NhibernateSample1

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: