亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Nhibernate學習起步之many-to-one篇

系統 1695 0
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; 代碼如下

Salary.cs
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> using System;
using System.Collections.Generic;
using System.Text;

namespace NhibernateSample1
{
public partial class Salary
{
int _id;
User_user;
int _year;
int _month;
int _envy;
decimal _money;
/**/ /// <summary>
/// 工資編號
/// </summary>

public virtual int Id
{
get
{
return _id;
}

set
{
_id
= value;
}

}

/**/ /// <summary>
/// 雇員
/// </summary>

public virtual UserEmployee
{
get
{
return _user;
}

set
{
_user
= value;
}

}

/**/ /// <summary>
/// 年度
/// </summary>

public int Year
{
get
{
return _year;
}

set
{
_year
= value;
}

}

/**/ /// <summary>
/// 月份
/// </summary>

public int Month
{
get
{
return _month;
}

set
{
_month
= value;
}

}

/**/ /// <summary>
/// 季度
/// </summary>

public int Envy
{
get
{
return _envy;
}

set
{
_envy
= value;
}

}

/**/ /// <summary>
/// 工資
/// </summary>

public decimal Money
{
get
{
return _money;
}

set
{
_money
= value;
}

}

}

}

3) 更改 User.cs, User 里面添加 SalaryList 屬性:

User.cs
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 private System.Collections.IList_salaryList;
2 /**/ /// <summary>
3 /// 工資列表
4 /// </summary>

5 public System.Collections.IListSalaryList
6 {
7 get
8 {
9 return _salaryList;
10 }

11 set
12 {
13 _salaryList = value;
14 }

15 }
4)修改 User.hbm.xml ,加入 bag 節點
User.hbm.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> < bagname = " SalaryList " table = " Salary " inverse = " true " lazy = " true " cascade = " all " >
< keycolumn = " Id " />
< one - to - many class = " NhibernateSample1.Salary,NhibernateSample1 " ></ one - to - many >
</ bag >

5 )編寫類 Salary 的映射文件 :Salary.hbm.xml
Salary.hbm.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
< hibernate - mappingxmlns = " urn:nhibernate-mapping-2.2 " >
< class name = " NhibernateSample1.Salary,NhibernateSample1 " table = " Salary " lazy = " false " >
< idname = " Id " column = " Id " unsaved - value = " 0 " >
< generator class = " native " />
</ id >
< propertyname = " Year " column = " Year " type = " Int32 " not - null = " true " ></ property >
< propertyname = " Month " column = " Month " type = " Int32 " not - null = " true " ></ property >
< propertyname = " Envy " column = " Envy " type = " Int32 " not - null = " true " ></ property >
< propertyname = " Money " column = " Money " type = " Decimal " not - null = " true " ></ property >
< many - to - onename = " Employee " column = " Uid " not - null = " true " ></ many - to - one >
</ class >
</ hibernate - mapping >
6 )編寫 CRUD
UserSalaryFixure.cs
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace NhibernateSample1
{
public class UserSalaryFixure
{
private ISessionFactory_sessions;
public void Configure()
{
Configurationcfg
= GetConfiguration();
_sessions
= cfg.BuildSessionFactory();
}

ConfigurationGetConfiguration()
{
string cfgPath = @" E:/myproject/nhibernatestudy/simle1/NHibernateStudy1/NhibernateSample1/hibernate.cfg.xml " ;
Configurationcfg
= new Configuration().Configure(cfgPath);
return cfg;
}

public void ExportTables()
{
Configurationcfg
= GetConfiguration();
new SchemaExport(cfg).Create( true , true );
}

public UserCreateUser(Stringname, string pwd)
{
Useru
= new User();
u.Name
= name;
u.Pwd
= pwd;
u.SalaryList
= new ArrayList();

ISessionsession
= _sessions.OpenSession();

ITransactiontx
= null ;

try
{
tx
= session.BeginTransaction();
session.Save(u);
tx.Commit();
}

catch (HibernateExceptione)
{
if (tx != null )tx.Rollback();
throw e;
}

finally
{
session.Close();
}


return u;
}

public SalaryCreateSalary(Useru, int year, int month, int envy, decimal money)
{
Salaryitem
= new Salary();
item.Year
= year;
item.Money
= money;
item.Envy
= envy;
item.Month
= month;
item.Employee
= u;
u.SalaryList.Add(item);
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;

try
{
tx
= session.BeginTransaction();
session.Update(u);
tx.Commit();
}

catch (HibernateExceptione)
{
if (tx != null )tx.Rollback();
throw e;
}

finally
{
session.Close();
}

return item;
}

public SalaryCreateSalary( int uid, int year, int month, int envy, decimal money)
{
Salaryitem
= new Salary();
item.Year
= year;
item.Money
= money;
item.Envy
= envy;
item.Month
= month;

ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Useru
= (User)session.Load( typeof (User),uid);
item.Employee
= u;
u.SalaryList.Add(item);
tx.Commit();
}

catch (HibernateExceptione)
{
if (tx != null )tx.Rollback();
throw e;
}

finally
{
session.Close();
}

return item;
}

public SalaryGetSalary( int salaryID)
{
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Salaryitem
= (Salary)session.Load( typeof (Salary),
salaryID);
tx.Commit();
return item;
}

catch (HibernateExceptione)
{
if (tx != null )tx.Rollback();
return null ;
}

finally
{
session.Close();
}

return null ;
}

public UserGetUser( int uid)
{
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Useritem
= (User)session.Load( typeof (User),
uid);
tx.Commit();
return item;
}

catch (HibernateExceptione)
{
if (tx != null )tx.Rollback();
return null ;
}

finally
{
session.Close();
}

return null ;
}

public void UpdateSalary( int salaryID, decimal money)
{
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Salaryitem
= (Salary)session.Load( typeof (Salary),
salaryID);
item.Money
= money;
tx.Commit();
}

catch (HibernateExceptione)
{
if (tx != null )tx.Rollback();
throw e;
}

finally
{
session.Close();
}

}


public void Delete( int uid)
{
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Salaryitem
= session.Load( typeof (Salary),uid) as Salary;;
session.Delete(item);
tx.Commit();
}

catch (HibernateExceptione)
{
if (tx != null )tx.Rollback();
throw e;
}

finally
{
session.Close();
}

}


}

}

7) 編寫單元測試類: UnitTest1.cs
UnitTest1.cs
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NhibernateSample1;

namespace TestProject1
{
/**/ /// <summary>
/// UnitTest1的摘要說明
/// </summary>

[TestClass]
public class UnitTest1
{
public UnitTest1()
{
//
// TODO:在此處添加構造函數邏輯
//
}

NhibernateSample1.UserSalaryFixureusf
= new UserSalaryFixure();
其他測試屬性 #region 其他測試屬性
//
// 您可以在編寫測試時使用下列其他屬性:
//
// 在運行類中的第一個測試之前使用ClassInitialize運行代碼
// [ClassInitialize()]
// publicstaticvoidMyClassInitialize(TestContexttestContext){}
//
// 在類中的所有測試都已運行之后使用ClassCleanup運行代碼
// [ClassCleanup()]
// publicstaticvoidMyClassCleanup(){}
//
// 在運行每個測試之前使用TestInitialize運行代碼
// [TestInitialize()]
// publicvoidMyTestInitialize(){}
//
// 在運行每個測試之后使用TestCleanup運行代碼
// [TestCleanup()]
// publicvoidMyTestCleanup(){}
//
#endregion


[TestMethod]
public void Test1()
{
usf.Configure();
usf.ExportTables();
Useru
= usf.CreateUser(Guid.NewGuid().ToString(), " ds " );
Assert.IsTrue(u.Id
> 0 );
Salarys
= usf.CreateSalary(u, 2007 , 3 , 1 ,( decimal ) 8000.00 );
Assert.IsTrue(s.Id
> 0 );
Salarys1
= usf.CreateSalary(u.Id, 2007 , 3 , 1 ,( decimal ) 7500 );
Assert.IsTrue(s1.Id
> 0 );
usf.UpdateSalary(s1.Id,(
decimal ) 6000 );
s1
= usf.GetSalary(s1.Id);
Assert.IsTrue(s1.Money
== ( decimal ) 6000 );
usf.Delete(s1.Id);
s1
= usf.GetSalary(s1.Id);
Assert.IsNull(s1);
Useru1
= usf.GetUser( 1 );
Assert.IsTrue(u1.SalaryList.Count
> 0 );
}


}

}

加載測試元數據,直到Test()通過。
總結:通過進一步學習nhiberate,發現ORM框架真是非常強大。今天先到這里。明天繼續。
項目文件: /Files/jillzhang/simple2.rar

Nhibernate學習起步之many-to-one篇


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 四虎影视在线影院4hutv | 中文在线1区二区六区 | 亚洲精品字幕一区二区三区 | 国产精品网站 夜色 | 99热这里只有精品国产在热久久 | 国产福利第一页 | 久久是免费只精品热在线 | 国产男女性特黄录像 | 亚洲乱亚洲乱妇无码 | 久久精品国产亚洲欧美 | 女色狠xx网18 | 久久国产一久久高清 | 国产欧美在线视频免费 | 国产美女在线观看 | 中日韩欧美一级毛片 | 日韩国产在线 | 欧美一级录像 | 久久久久久网 | 亚洲视频一二三 | 亚洲精品在线视频观看 | 国产乱码精品一区二区三区四川 | 久久精品亚洲欧美日韩久久 | 99精品这里只有精品高清视频 | 欧美亚洲综合图区在线 | 黄色在线观看www | 青草91| 在线性视频| 精品亚洲无人区一区二区 | 久久亚洲精品久久久久 | 色婷婷视频在线观看 | 99色视频| 久久精品国产亚洲网址 | 日韩在线免费视频 | 欧美日本免费观看αv片 | 视色视频在线 | 亚洲精品影院 | 久久久久国产精品免费免费不卡 | 在线99| 黄色操视频| 日日爱夜夜操 | 久久天堂夜夜一本婷婷麻豆 |