package com.wyq.hibernate;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
public class EMailList implements UserType {
private List emails;
private static final char SPLITTER = ';';
private static final int[] TYPES = new int[]{Types.VARCHAR};
public boolean isMutable() {
return false;
}
public int[] sqlTypes() {
return TYPES;
}
public Class returnedClass() {
return List.class;
}
/**
* 创建一个新的List实例,包含原有List实例中的所有元素。
*/
public Object deepCopy(Object value) throws HibernateException {
List sourcelist = (List)value;
List targetlist = new ArrayList();
targetlist.addAll(sourcelist);
return targetlist;
}
/**
* 判断email list是否发生改变
*/
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();i++){
String str1 = (String)xList.get(i);
String str2 = (String)yList.get(i);
if(!str1.equals(str2))return false;
}
return true;
}
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;
}
}
/**
* 将List型的email信息组成字符串后保存到email字段
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
System.out.println("Set method executed");
if(value!=null){
String str = "";//assemble((List)value);
Hibernate.STRING.nullSafeSet(st, str, index);
}else{
Hibernate.STRING.nullSafeSet(st, value, index);
}
}
/**
* 将String拼装为一个字符串,以";"分隔
*/
private String assemble(List emailList){
StringBuffer strBuf = new StringBuffer();
for(int i=0;i<emailList.size()-1;i++){
strBuf.append(emailList.get(i)).append(SPLITTER);
}
strBuf.append(emailList.get(emailList.size()-1));
return strBuf.toString();
}
/**
* 将以";"分隔的字符串解析为一个字符串数组
*/
private List parse(String value){
String[] strs = org.apache.commons.lang.StringUtils.split(value,String.valueOf(SPLITTER));
List emailList = new ArrayList();
for(int i=0;i<strs.length;i++){
emailList.add(strs[i]);
}
return emailList;
}
public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public Serializable disassemble(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public int hashCode(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return 0;
}
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}
}
posted on 2009-10-16 16:57
王永庆 阅读(151)
评论(0) 编辑 收藏 所属分类:
HIBERNATE