读书人

使用hibernate中的UserType解决乱码有

发布时间: 2013-01-26 13:47:02 作者: rapoo

使用hibernate中的UserType解决乱码问题

转自:http://hi.baidu.com/gacmotor/item/3ea2e62952c99acbddf69aea总的思想还是一样:
页面编码都是utf-8,数据库是xxx,那就new String("你好".getBytes("utf-8"),"xxx")然后读取出来的时候new String("...".getBytes("xxx"),"utf-8")
?hibernate 访问 oracle 乱码 oracle的字符集是WE8ISO8859P1,由于历史原因,不可修改。已经修改本地 NLS_LANG,因此使用PL/SQL developer可以正常访问。 但是 hibernate使用thin方式连接数据库,中文乱码。 为了使页面可以正常显示,在取数据时在form的get方法中使用 String new_str = new String(old_str.getBytes("iso-8859-1")); 得到的new_str可以正常显示中文。 但是这样增加了冗余代码,而且使用好多第三方组件时,由于无法控制转码细节,导致乱码。 在互联网上搜索解决方案,搜到的大多是一个方法,加入filter,使用org.springframework.web.filter.CharacterEncodingFilter 但是对我这个情况完全无效,存取数据都是乱码,存到数据库里的数用客户端看也是乱码。 我觉得CharacterEncodingFilter应该是在oracle的字符集正确设置的情况下,比如使用ZHS16GBK,解决乱码的问题。 现在请教各位高手有没有遇到过同样的问题,如何解决这种情况下的乱码。是否需要写一个filter或者是hibernate直接可以对oracle字符集进行配置。 问题已经解决,说一下我的解决方案吧。使用hibernate的UserType 在hbm.xml中配置,比如 <property name="roleName" type="com.xtmcc.framework.dao.GBKString"> <column name="ROLE_NAME" length="50" /> </property> 自定义GBKString 类
package two;import java.io.Serializable;import java.io.UnsupportedEncodingException;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import oracle.jdbc.driver.OracleTypes;import org.apache.commons.lang.builder.HashCodeBuilder;import org.hibernate.HibernateException;import org.hibernate.usertype.UserType;public class a1 implements UserType {public a1() {super();}public int[] sqlTypes() {return new int[] { OracleTypes.VARCHAR };}public Class returnedClass() {return String.class;}public boolean equals(Object x, Object y) throws HibernateException {return (x == y) || (x != null && y != null && (x.equals(y)));}public Object nullSafeGet(ResultSet rs, String[] names, Object owner)throws HibernateException, SQLException {String val = rs.getString(names[0]);if (null == val) {return null;} else {try {val = new String(val.getBytes("iso-8859-1"), "GBK");} catch (UnsupportedEncodingException e) {throw new HibernateException(e.getMessage());}return val;}}public void nullSafeSet(PreparedStatement st, Object value, int index)throws HibernateException, SQLException {if (value == null) {st.setNull(index, OracleTypes.VARCHAR);} else {String val = (String) value;try {val = new String(val.getBytes("GBK"), "ISO_8859_1");} catch (UnsupportedEncodingException e) {throw new HibernateException(e.getMessage());}st.setObject(index, val, OracleTypes.VARCHAR);}}public Object deepCopy(Object value) throws HibernateException {if (value == null)return null;return new String((String) value);}public boolean isMutable() {return false;}public Object assemble(Serializable arg0, Object arg1)throws HibernateException {// TODO Auto-generated method stubreturn null;}public Serializable disassemble(Object arg0) throws HibernateException {// TODO Auto-generated method stubreturn null;}public int hashCode(Object arg0) throws HibernateException {return HashCodeBuilder.reflectionHashCode(this);}public Object replace(Object arg0, Object arg1, Object arg2)throws HibernateException {// TODO Auto-generated method stub//return null;}}
?

读书人网 >软件架构设计

热点推荐