免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 938 | 回复: 0
打印 上一主题 下一主题

深入浅出Hibernate之UserType接口的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-27 09:44 |只看该作者 |倒序浏览
问题提出:某用户的EMail地址可以选择多个,实现UserType接口,封装对此EMail字段的使用,数据库中,各个地址用“;”隔开;

UserType interface:
public interface UserType {
    public int[] sqlTypes();

    public Class returnedClass();

    public boolean equals(Object x, Object y) throws HibernateException;
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException;

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException;
    public Object deepCopy(Object value) throws HibernateException;
    public boolean isMutable();
}

用EMailList实现:

import java.util.*;
import java.sql.*;
public class EMailList implements UserType{

private List emails;

private static final char SPLITTER = ';';

private static final int[] TYPES = new int[]{Types.VARCHAR};;

public int[] sqlTypes(){
  
  return TYPES;
}

public Class returnedClass(){
  
  return List.class;
}

public Object deepCopy(Object value) throws HibernateException{
  
  List sourcelist = (List)value;
  
  List targetlist = new ArrayList();
  
  targetlist.addAll(sourcelist);
  
  return targetlist;
  
}

/*
  * 判断emial 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
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

配置TUser.hbm.xml文件

   
        
            
            
        
        
            
        
        
            
        
        
            
        
   

对应的TUser.java文件

package org.hibernatetest.bean;
import java.util.*;
/**
* TUser generated by MyEclipse - Hibernate Tools
*/
public class TUser  implements java.io.Serializable {
    // Fields   
     private Integer id;
     private String name;
     private Integer age;
     private List email;
    // Constructors
    /** default constructor */
    public TUser() {
    }
   
    /** full constructor */
    public TUser(String name, Integer age, List email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }
   
    // Property accessors
    public Integer getId() {
        return this.id;
    }
   
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return this.age;
    }
   
    public void setAge(Integer age) {
        this.age = age;
    }
    public List getEmail() {
     if(email == null)
      
      this.email = new ArrayList();
     
     return email;
    }
   
    public void setEmail(List email) {
        this.email = email;
    }
   
}

使用:

public class TuserDAO {
public void addTuser(TUser tuser) {
  try {
   Session session = HibernateSessionFactory.getSession();
   
   Transaction tx = session.beginTransaction();
   tuser.setName("xiaosan001");
   
   List list = tuser.getEmail();
      
   list.add(0, "
7149614@qq.com
");
   
   list.add(1,"
jianmin@qq.com
");
   session.save(tuser);
   session.flush();
   tx.commit();
   
  } catch (Exception ex) {
   
   System.out.println(ex.toString());
  }
}

public void select(){
  
  String hql = "from TUser where id > 0";
  try{
   
   Session session = HibernateSessionFactory.getSession();
   
   List userList = session.createQuery(hql).list();
   
   TUser tuser = null;
   
   for(int i=0;i
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15642/showart_368461.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP