开篇
用NHibernate持久化类的enum和bool类型的属性的时候,我们需要持久化的更通用一些,数据库无关一些。
枚举持久化为对应整数的字符串,NH默认持久化为整数。
布尔的持久化默认根据数据库的类型而不同,sqlserver会持久化为bit,可是很多数据库没有bit类型,通常用char来持久化。
可以选择持久化为Y和N,也可以选择持久化为1和0。
1、枚举enum的持久化
将枚举持久化成一个字符串,也就是将枚举对应的整数持久化到数据库。
1
2
3
4
5
|
public
enum
Status
{
Yes = 1,
No = 2
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
class
Product
{
public
Product()
{
this
.CreateTime = DateTime.Now;
this
.ModifyTime = DateTime.Now;
}
public
virtual
Guid Id {
get
;
set
; }
public
virtual
string
Name {
get
;
set
; }
public
virtual
bool
Discontinued {
get
;
set
; }
public
virtual
DateTime CreateTime {
get
;
set
; }
public
virtual
DateTime ModifyTime {
get
;
set
; }
public
virtual
Category Category {
get
;
set
; }
public
virtual
int
Version {
get
;
set
; }
public
virtual
bool
IsDelete {
get
;
set
; }
public
virtual
Status Status {
get
;
set
; }
}
|
直接在column中加入sql-type属性,设置为数据库列对应的类型就可以了。
1
2
3
|
<
property
name
=
"Status"
>
<
column
name
=
"Status"
sql-type
=
"varchar(10)"
default
=
"1"
not-null
=
"true"
></
column
>
</
property
>
|
2、布尔bool的持久化
2.1 持久化为Y和N
1
2
3
4
|
<
property
name
=
"IsDelete"
type
=
"NHibernate.Type.YesNoType, NHibernate"
column
=
"IsDelete"
>
<
column
name
=
"IsDelete"
sql-type
=
"char(1)"
default
=
"0"
not-null
=
"true"
></
column
>
</property
|
使用上面的配置,在数据库存储的就是Y和N,代表true和false。
2.2 持久化为1和0
但是我们有时候想要使用1和0代表true和false。
那么可以先自定义下面的类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
public
class
YesNo10 : IUserType
{
public
bool
IsMutable
{
get
{
return
false
; }
}
public
Type ReturnedType
{
get
{
return
typeof
(YesNoType); }
}
public
SqlType[] SqlTypes
{
get
{
return
new
[] { NHibernateUtil.String.SqlType }; }
}
public
object
NullSafeGet(IDataReader rs,
string
[] names,
object
owner)
{
var
obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if
(obj ==
null
)
return
null
;
var
yesNo = (
string
)obj;
if
(yesNo !=
"1"
&& yesNo !=
"0"
)
throw
new
Exception(
string
.Format (
"Expected data to be '1' or '0' but was '{0}'."
, yesNo));
return
yesNo ==
"1"
;
}
public
void
NullSafeSet(IDbCommand cmd,
object
value,
int
index)
{
if
(value ==
null
)
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
var
yes = (
bool
)value;
((IDataParameter)cmd.Parameters[index]).Value = yes ?
"1"
:
"0"
;
}
}
public
object
DeepCopy(
object
value)
{
return
value;
}
public
object
Replace(
object
original,
object
target,
object
owner)
{
return
original;
}
public
object
Assemble(
object
cached,
object
owner)
{
return
cached;
}
public
object
Disassemble(
object
value)
{
return
value;
}
public
new
bool
Equals(
object
x,
object
y)
{
if
(ReferenceEquals(x, y))
return
true
;
if
(x ==
null
|| y ==
null
)
return
false
;
return
x.Equals(y);
}
public
int
GetHashCode(
object
x)
{
return
x ==
null
?
typeof
(
bool
).GetHashCode() + 473 : x.GetHashCode();
}
}
|
然后配置成下面的样子。
1
2
3
|
<
property
name
=
"IsDelete"
type
=
"Com.Andyshi.NH3.Domain.YesNo10, Com.Andyshi.NH3"
column
=
"IsDelete"
>
<
column
name
=
"IsDelete"
sql-type
=
"char(1)"
default
=
"0"
not-null
=
"true"
></
column
>
</property
|
type中的内容是“类名, 程序集名”。
本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/1229651,如需转载请自行联系原作者