关于网友提出的“ 请问在hibernate中怎样把mysql的varbinary映射成String?”问题疑问,本网通过在网上对“ 请问在hibernate中怎样把mysql的varbinary映射成String?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 请问在hibernate中怎样把mysql的varbinary映射成String?
描述: 如题
要自己定义映射类型吗?
解决方案1: 自己写代码吧,实现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);
}
以上介绍了“ 请问在hibernate中怎样把mysql的varbinary映射成String?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3424099.html