为什么要这么做? 
如果是字符串,干嘛不定义成 varchar?

解决方案 »

  1.   

    这是因为好像用varchar的时候
    mysql好像不支持中文按拼音排序
    而且我们的系统不可能改成varchar了
    否则要做比较大的改动
      

  2.   

    自己写代码吧,实现UserType接口参考一下——public class VarbinaryType implements UserType {

    private static final int[] SQL_TYPES = {Types.VARBINARY};

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

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

    public boolean isMutable() { return false; }

    public Object deepCopy(Object value) { return value; }

    public boolean equals(Object x,Object y) {
    if (x == y) return true;
    if (x == null || y == null) return false;
    return x.equals(y);
    }

    public int hashCode(Object x) { return x.hashCode();}

    public Object nullSafeGet(ResultSet resultSet, String[] names,Object owner) throws HibernateException, SQLException {
    if(resultSet.wasNull()) return null;
    byte[] tempValue = resultSet.getBytes(names[0]);
    String value = new String(tempValue);
    return value;
    }

    public void nullSafeSet(PreparedStatement statement,Object value, int index) throws HibernateException,SQLException {
    if(value == null) {
    statement.setNull(index,Types.VARCHAR);
    }
    else {
    byte[] tempValue = ((String)value).getBytes();
    statement.setBytes(index,tempValue);
    }
    }

    public Serializable disassemble(Object value) throws HibernateException { 
    return (Serializable) deepCopy(value); 
    }

    public Object assemble(Serializable cached, Object owner) throws HibernateException { 
         return deepCopy(cached); 
        }  public Object replace(Object original, Object target, Object owner) throws HibernateException { 
    return deepCopy(original); 
    }