posts - 66,  comments - 11,  trackbacks - 0
          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;
              }
              
          /**
               * 創(chuàng)建一個新的List實例,包含原有List實例中的所有元素。
               
          */
              
          public Object deepCopy(Object value) throws HibernateException {
                  List sourcelist 
          = (List)value;
                  List targetlist 
          = new ArrayList();
                  targetlist.addAll(sourcelist);
                  
          return targetlist;
              }
              
          /**
               * 判斷email list是否發(fā)生改變
               
          */
              
          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();
              }
              
          /**
               * 將以";"分隔的字符串解析為一個字符串數(shù)組
               
          */
              
          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 王永慶 閱讀(163) 評論(0)  編輯  收藏 所屬分類: HIBERNATE
          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          關(guān)注blogs

          搜索

          •  

          最新評論

          • 1.?re: 心中的那個她[未登錄]
          • 如果心情能那么容易放開就好了,碰到她是我的緣分,但是她的緣分不知道在哪里。
          • --王永慶
          • 2.?re: 心中的那個她[未登錄]
          • 兄弟,喜歡就大膽去追好了,就是因為喜歡才會有不愉快,如果想一輩子沒有這些煩惱,找一個可有可無的人就好了。這種事講的是緣分吧。別人對你好不好其實不太重要。放開心情就好了。
          • --萍水相逢
          • 3.?re: 心中的那個她
          • 只能同甘不能共苦,就他媽算了吧,程序員也是人?。∥椰F(xiàn)在也很討厭女的不理解人情。
          • --咖啡妝
          • 4.?re: 難以割舍的那份情感
          • 評論內(nèi)容較長,點擊標題查看
          • --王永慶
          • 5.?re: 難以割舍的那份情感
          • 評論內(nèi)容較長,點擊標題查看
          • --隔葉黃鶯

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 呼图壁县| 萝北县| 定陶县| 乌审旗| 南召县| 老河口市| 永川市| 萨迦县| 凯里市| 宜昌市| 莱阳市| 宝坻区| 新邵县| 宁津县| 金昌市| 冕宁县| 临猗县| 普宁市| 彰化县| 墨竹工卡县| 丁青县| 双城市| 连州市| 锡林郭勒盟| 灯塔市| 芜湖县| 桂林市| 德昌县| 保山市| 鄂温| 商南县| 康乐县| 昭苏县| 静宁县| 成安县| 洛川县| 罗田县| 邯郸市| 马关县| 遂平县| 望都县|