UserType and CompositeUserType為
Hibernate中提供的用戶類型自定義接口。根據這個接口,可以實現自定義的數據類型。
最近看<<深入淺出
Hibernate>>,作者在書中介紹了
Hibernate 提供的用戶自定義數據類型,于是效仿書中的方法,在數據庫表中添加一個字段用于記錄所有好友的userid,每個userid之間用";"加以分隔,同時將該字段映射為一個特殊的List集合,利用UserType interface實現String解析后將各個userid封裝在List中,將List中的記錄封裝成以";"分隔的String。
使用UserType接口,實現了較好的設計風格,以及更好的重用性。
/*
* 用戶自定義的數據類型,對應數據庫中的一個字段,在該字段中,保存了
* 多個用戶需要的信息,之間用";"加以分隔.
* @Author:Paul
* @Date:April 18th,2008
*/
package com.globalhands.
hibernate.userTypes;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.Types;
import org.
hibernate.
Hibernate;
import org.
hibernate.HibernateException;
import org.
hibernate.usertype.UserType;
public class SpecialList implements UserType {
private List specialList;
private static final char SPLITTER = ';';
private static final int[] TYPES = new int[] { Types.VARCHAR };
public String assemble(Serializable arg0, Object arg1)
throws HibernateException {
return null;
}
/*
* 將List封裝為一個String對象
*/
public String assemble(List specialList) throws HibernateException {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < specialList.size() - 1; i++) {
sb.append(specialList.get(i)).append(this.SPLITTER);
}
sb.append(specialList.get(specialList.size() - 1));
return sb.toString();
}
/*
* 創建一個新的List實例,包含原有的List實例中的所有元素.
*/
public Object deepCopy(Object value) throws HibernateException {
List sourceList = (List) value;
List targetList = new ArrayList();
targetList.addAll(sourceList);
return targetList;
}
public Serializable disassemble(Object arg0) throws HibernateException {
return null;
}
/*
* 判斷specialList是否發生變化
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
}
if (x != null && y != null) {
List xList = (List) x;
List yList = (List) y;
if (xList.size() != yList.size()) {
return false;
}
for (int i = 0; i <= xList.size() - 1; i++) {
String str1 = (String) xList.get(i);
String str2 = (String) yList.get(i);
if (!xList.equals(yList)) {
return false;
}
}
return true;
}
return false;
}
public int hashCode(Object arg0) throws HibernateException {
return 0;
}
public boolean isMutable() {
return false;
}
/*
* 從resultset中取出email字段,并將其解析為List類型后返回
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
String value = (String)
Hibernate.STRING.nullSafeGet(rs, names[0]);
if (value != null) {
return parse(value);
} else {
return null;
}
}
/*
* 將以";"分隔的字符串解析為一個字符串數組
*/
private List parse(String value) {
String[] strs = value.split(";");
List specialList = new ArrayList();
for (int i = 0; i <= strs.length - 1; i++) {
specialList.add(strs[i]);
}
return specialList;
}
/*
* 將List型的email信息組裝成字符串之后保存到email字段
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (value != null) {
String str = assemble((List) value);
Hibernate.STRING.nullSafeSet(st, str, index);
} else {
Hibernate.STRING.nullSafeSet(st, value, index);
}
}
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return null;
}
public Class returnedClass() {
return List.class;
}
public int[] sqlTypes() {
return TYPES;
}
}
同時,修改相應的[ormapping_filename].hbm.xml中相應字段的映射信息
<property name="buddy"
type="com.globalhands.hibernate.userTypes.SpecialList">
<column name="buddy" length="2000" not-null="true" />
</property>
之后,修改POJO類中該字段的返回類型為List。
使用JUnit測試程序。
posted @
2009-05-24 19:57 lanxin1020 閱讀(591) |
評論 (0) |
編輯 收藏
posted @
2009-05-21 22:13 lanxin1020 閱讀(159) |
評論 (0) |
編輯 收藏