User.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
class
User
{
private
int
_id;
private
string
_name;
private
string
_pwd;
private
System.Collections.IList_departmentsList;
/**/
///
<summary>
///
编号
///
</summary>
public
virtual
int
Id
{
get
{
return
_id;
}
set
{
_id
=
value;
}
}
/**/
///
<summary>
///
名称
///
</summary>
public
virtual
string
Name
{
get
{
return
_name;
}
set
{
_name
=
value;
}
}
/**/
///
<summary>
///
密码
///
</summary>
public
virtual
string
Pwd
{
get
{
return
_pwd;
}
set
{
_pwd
=
value;
}
}
/**/
///
<summary>
///
工资列表
///
</summary>
public
System.Collections.IListDepartmentsList
{
get
{
return
_departmentsList;
}
set
{
_departmentsList
=
value;
}
}
}
}
2)User.hbm.xml
User.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.User,NhibernateSample1
"
table
=
"
Users
"
lazy
=
"
false
"
>
<
idname
=
"
Id
"
column
=
"
Id
"
unsaved
-
value
=
"
0
"
>
<
generator
class
=
"
native
"
/>
</
id
>
<
propertyname
=
"
Name
"
column
=
"
Name
"
type
=
"
string
"
length
=
"
64
"
not
-
null
=
"
true
"
unique
=
"
true
"
></
property
>
<
propertyname
=
"
Pwd
"
column
=
"
Pwd
"
type
=
"
string
"
length
=
"
64
"
not
-
null
=
"
true
"
></
property
>
<
bagname
=
"
DepartmentsList
"
table
=
"
Users_Departments
"
inverse
=
"
true
"
lazy
=
"
false
"
cascade
=
"
all
"
>
<
keycolumn
=
"
Id
"
/>
<
many
-
to
-
many
class
=
"
NhibernateSample1.Departments,NhibernateSample1
"
column
=
"
DepID
"
></
many
-
to
-
many
>
</
bag
>
</
class
>
</
hibernate
-
mapping
>
3) Departments.cs
Departments
<!--<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;
namespace
NhibernateSample1
{
public
class
Departments
{
int
_depID;
string
_name;
IList_usersList
=
new
ArrayList();
public
virtual
int
DepID
{
get
{
return
_depID;
}
set
{
_depID
=
value;
}
}
public
virtual
string
Name
{
get
{
return
_name;
}
set
{
_name
=
value;
}
}
public
virtual
IListUsersList
{
get
{
return
_usersList;
}
set
{
_usersList
=
value;
}
}
}
}
4) Departments.hbm.xml
Departments.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.Departments,NhibernateSample1
"
table
=
"
Departments
"
lazy
=
"
false
"
>
<
idname
=
"
DepID
"
column
=
"
DepID
"
unsaved
-
value
=
"
0
"
>
<
generator
class
=
"
native
"
/>
</
id
>
<
propertyname
=
"
Name
"
column
=
"
Name
"
type
=
"
string
"
length
=
"
64
"
not
-
null
=
"
true
"
unique
=
"
true
"
></
property
>
<
bagname
=
"
UsersList
"
table
=
"
Users_Departments
"
lazy
=
"
true
"
>
<
keycolumn
=
"
DepID
"
/>
<
many
-
to
-
many
class
=
"
NhibernateSample1.User,NhibernateSample1
"
column
=
"
Id
"
></
many
-
to
-
many
>
</
bag
>
</
class
>
</
hibernate
-
mapping
>
5) 数据操作类
UserDepartmentFixure.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
UserDepartmentFixure
{
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.DepartmentsList
=
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
DepartmentsCreateDepartments(Useru,
string
name)
{
Departmentsitem
=
new
Departments();
item.Name
=
name;
u.DepartmentsList.Add(item);
item.UsersList.Add(u);
ISessionsession
=
_sessions.OpenSession();
ITransactiontx
=
null
;
try
{
tx
=
session.BeginTransaction();
session.Save(item);
tx.Commit();
}
catch
(HibernateExceptione)
{
if
(tx
!=
null
)tx.Rollback();
throw
e;
}
finally
{
session.Close();
}
return
item;
}
public
DepartmentsGetDepartments(
int
depID)
{
ISessionsession
=
_sessions.OpenSession();
ITransactiontx
=
null
;
try
{
tx
=
session.BeginTransaction();
Departmentsitem
=
(Departments)session.Load(
typeof
(Departments),
depID);
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
Delete(
int
uid)
{
ISessionsession
=
_sessions.OpenSession();
ITransactiontx
=
null
;
try
{
tx
=
session.BeginTransaction();
Departmentsitem
=
session.Load(
typeof
(Departments),uid)
as
Departments;
session.Delete(item);
tx.Commit();
}
catch
(HibernateExceptione)
{
if
(tx
!=
null
)tx.Rollback();
throw
e;
}
finally
{
session.Close();
}
}
}
}