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