[Grails] Grails中如何配置hibernate的自定义类型

lingzantia 2008-11-12
不用hbm.xml的话
lingzantia 2008-11-12
我用
static mapping = {
  columns{
   nameList type:hibernate.ListString
  }
}
没作用,谁有类似经验吗?
agile_boy 2008-11-13
http://www.iteye.com/topic/214145

介绍了如何使用Annotation来自定义UserType,不知你是怎样使用这个UserType的?
lingzantia 2008-11-13
userType代码如下:ListString.groovy,使用如
static mapping = {
   nameList type:hibernate.ListString
}

package hibernate
/**
 * Created by IntelliJ IDEA.
 * User: gd
 * Date: Nov 12, 2008
 * Time: 10:04:20 AM
 * To change this template use File | Settings | File Templates.
 */
import org.hibernate.usertype.UserType;
import org.hibernate.HibernateException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

class ListString implements UserType{
    private List specialList;

    private static final char SPLITTER = ',';
    private static final int[] TYPES = [ 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.is(y)) {
            return true;
        }
        if (x != null && y != null) {
            List xList = (List) x;
            List yList = (List) y;

            if (xList.size() != yList.size()) {
                return false;
            }

            if (xList != 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(this.SPLITTER);
        List theSpecialList = new ArrayList();
        for (int i = 0; i <= strs.length - 1; i++) {
            theSpecialList.add(strs[i]);
        }
        return theSpecialList;
    }

    /*
    * 将List型的信息组装成字符串之后保存到字段
    */
    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;
    }
}
lingzantia 2008-11-13
使用代码如下:
class InList{
    List value

    static constraints = {
        value(blank:false, nullable:true, validator:{ return it.size()>0 })
    }

    static mapping = {
        value type:hibernate.ListString
    }
}

groovy中如何使用hibernate的annotation呢?可以成功映射吗?
Global site tag (gtag.js) - Google Analytics