- 论坛徽章:
- 0
|
问题提出:某用户的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 |
|