网上关于HIBERNATE自定义类型的文章很少,今天自己测试了一下,顺便把代码帖在这里
package entiy;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public interface Email
{
public int [] sqlTypes(); //映射的字段类型
public Class returnedClass(); //返回的数据类型
public boolean equals(Object x,Object y); //脏数据检查时的方法
public Object nullSafeGet(ResultSet rs,String [] names,Object owern) throws Exception; //获得数据类型
public void nullSafeSet(PreparedStatement ps,Object value,int index)throws Exception; //设置数据
public Object deepCopy(Object value) throws Exception; //数据拷贝,用于在创建副本时调用
public boolean isMutable();
}

实际数据了类型
1
package entiy;
2
import java.io.Serializable;
3
import java.sql.PreparedStatement;
4
import java.sql.ResultSet;
5
import java.util.ArrayList;
6
import java.util.List;
7
8
import oracle.xdb.XMLType;
9
10
import org.apache.commons.lang.StringUtils;
11
import org.hibernate.Hibernate;
12
13
public class EmailList implements Email,Serializable
{ //一定要实现Serializable,否则报错: could'nt determint emalist type
14
15
private List emailList;
16
17
private static final char split = ';';
18
19
private static final int[] TYPES = new int[]
{oracle.xdb.XMLType._SQL_TYPECODE }; //不同的数据库,这里有不同:Types.varchar(mysql)
20
21
public int[] sqlTypes()
{
22
23
return TYPES;
24
}
25
26
public Class returnedClass()
{
27
28
return List.class;
29
30
}
31
32
public boolean equals(Object x, Object y)
{
33
34
if (x == y)
35
return true;
36
if (x != null || y != null)
{
37
List xList = (List) x;
38
List yList = (List) y;
39
40
if (xList.size() != yList.size())
41
return false;
42
for (int i = 0; i < xList.size(); i++)
{
43
String str1 = (String) xList.get(i);
44
String str2 = (String) yList.get(i);
45
46
if (!str1.equals(str2))
47
return false;
48
49
}
50
return true;
51
52
}
53
54
return false;
55
56
}
57
58
public Object nullSafeGet(ResultSet rs, String[] names, Object owern)
59
throws Exception
{
60
61
String value = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);
62
/**//*
63
* String value=(String) Hibernate.STRING.nullSafeGet(rs,names[0]);
64
* 是获取id列的值.
65
*
66
* 其实这个东西可以这样来看:JDBC代码 rs.getString("id");
67
*
68
*/
69
if (value != null)
70
return parse(value);
71
else
72
return null;
73
74
}
75
76
public void nullSafeSet(PreparedStatement ps, Object value, int index)
77
throws Exception
{
78
System.out.println("Set method execute");
79
if (value != null)
{
80
String str = assemble((List) value);
81
Hibernate.STRING.nullSafeSet(ps, str, index);
82
83
} else
84
Hibernate.STRING.nullSafeSet(ps, value, index);
85
86
}
87
88
public Object deepCopy(Object value) throws Exception
{
89
List sourceList = (List) value;
90
List targetList = new ArrayList();
91
targetList.addAll(sourceList);
92
return targetList;
93
94
}
95
96
public boolean isMutable()
{
97
98
return false;
99
}
100
101
public String assemble(List list)
{
102
103
StringBuffer strbuf = new StringBuffer();
104
for (int i = 0; i < list.size() - 1; i++)
{
105
106
strbuf.append((String) list.get(i)).append(split);
107
}
108
strbuf.append(list.get(list.size() - 1));
109
return strbuf.toString();
110
111
}
112
113
public List parse(String value)
{
114
115
String[] str = StringUtils.split(value, split);
116
List list = new ArrayList();
117
for (int i = 0; i < str.length; i++)
{
118
String email = str[i];
119
list.add(email);
120
121
}
122
return list;
123
124
}
125
126
}
127
配置文件
1
<?xml version="1.0"?>
2
<!DOCTYPE hibernate-mapping PUBLIC
3
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5
6
<hibernate-mapping
7
>
8
<class
9
name="entiy.Tuser"
10
table="Tuser"
11
dynamic-update="false"
12
dynamic-insert="false"
13
select-before-update="false"
14
optimistic-lock="version"
15
>
16
17
<id
18
name="id"
19
column="id"
20
type="int"
21
unsaved-value="0"
22
>
23
<generator class="sequence">
24
<param name="sequence">TUSER_SEQ</param>
25
</generator>
26
27
28
</id>
29
30
<property
31
name="name"
32
type="string"
33
update="true"
34
insert="true"
35
access="property"
36
column="name"
37
length="8"
38
not-null="false"
39
/>
40
41
42
<property
43
name="email"
44
type="entiy.EmailList" //这里要注意一定得引入oracle包,否则或抱错:couldnt determine the EmailList type
45
column="email"
46
47
/>
48
49
50
51
52
<!--
53
To add non XDoclet property mappings, create a file named
54
hibernate-properties-User.xml
55
containing the additional properties and place it in your merge dir.
56
-->
57
58
</class>
59
60
</hibernate-mapping>
61
注: 一定要引入.xmlparserv2.jar xdb.jar nls_charset12.jar 1
import java.io.File;
2
import java.util.List;
3
4
import junit.framework.Assert;
5
import junit.framework.TestCase;
6
7
import org.hibernate.Session;
8
import org.hibernate.SessionFactory;
9
import org.hibernate.Transaction;
10
import org.hibernate.cfg.Configuration;
11
12
import entiy.*;
13
14
public class hibernateTest extends TestCase
{
15
16
Session session = null;
17
18
protected void setUp() throws Exception
{
19
20
try
{
21
22
File flie = new File(
23
"D:/workspace/HiberanteSample/hibernate.cfg.xml");
24
Configuration config = new Configuration().configure(flie);
25
SessionFactory sessionfactory = config.buildSessionFactory();
26
27
session = sessionfactory.openSession();
28
29
} catch (Exception e)
{
30
e.printStackTrace();
31
}
32
33
}
34
35
protected void tearDown() throws Exception
{
36
37
super.tearDown();
38
}
39
40
41
42
public void testAddemail()
{
43
44
try
{
45
Tuser user = (Tuser) session.get(Tuser.class, new Integer(2));
46
List email = user.getEmail();
47
email.add("chang@163.com");
48
Transaction tarn = session.beginTransaction();
49
session.save(user);
50
tarn.commit();
51
52
53
} catch (Exception e)
{
54
e.printStackTrace();
55
}
56
57
}
58
59
}
60