使用Gson转换hibernate对象遇到一个问题,当对象的Lazy加载的,就会出现上面的错误。处理方式摘抄自网上,留存一份以后自己看。

网上找到的解决办法,首先自定义一个类继承TypeAdapter:

 package cn.xjy.finance.loanapply.utils ;

 import java.io.IOException ;
import org.hibernate.Hibernate ;
import org.hibernate.proxy.HibernateProxy ;
import com.google.gson.Gson ;
import com.google.gson.TypeAdapter ;
import com.google.gson.TypeAdapterFactory ;
import com.google.gson.reflect.TypeToken ;
import com.google.gson.stream.JsonReader ;
import com.google.gson.stream.JsonWriter ; /**
* This TypeAdapter unproxies Hibernate proxied objects, and serializes them
* through the registered (or default) TypeAdapter of the base class.
*/
public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> { public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson,
TypeToken<T> type) {
return (HibernateProxy.class
.isAssignableFrom(type
.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(
gson) : null) ;
}
} ; private final Gson context ; private HibernateProxyTypeAdapter(Gson context) {
this.context = context ;
} @Override
public HibernateProxy read(JsonReader in) throws IOException {
throw new UnsupportedOperationException("Not supported") ;
} @SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
if (value == null) {
out.nullValue() ;
return ;
}
// Retrieve the original (not proxy) class
Class<?> baseType = Hibernate.getClass(value) ;
// Get the TypeAdapter of the original class, to delegate the
// serialization
TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType)) ;
// Get a filled instance of the original class
Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
.getImplementation() ;
// Serialize the value
delegate.write(out, unproxiedValue) ;
}
}

然后初始化gson对象的方式:

 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd")
.registerTypeAdapterFactory(
HibernateProxyTypeAdapter.FACTORY).create() ;

这样就解决了,出现这个错误的主要原因是,hibernate采取懒加载的方式查询数据库,也就是只有用到了才去查真正的数据,用不到的话只是返回一个代理对象,gson识别不了代理对象,所以没法转换.

GSON 报错HibernateProxy. Forgot to register a type adapter? 的解决办法的更多相关文章

  1. Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

    当我们使用gson 转对象时,并且这个对象中有一些属性是懒加载时如 @Entity @Table(name = "user") public class User { @Id @C ...

  2. HibernateProxy异常处理 java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

    这里使用google的Gson包做JSON转换,因为较早的1.4版本的FieldAttributes类中没有getDeclaringClass()这个方法,这个方法是获取field所属的类,在我的排除 ...

  3. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  4. SQL Developer报错:Unable to find a Java Virtual Machine解决办法

    安装了64位的Oracle数据库以及32位的Oracle客户端,在开始菜单中第一次打开客户端的SQL Developer时提示输入java.exe的路径,我选择了Oracle数据库自带的jdk路径,确 ...

  5. 关于报错The specified child already has a parent的解决办法

    报错信息为:java.lang.IllegalStateException: The specified child already has a parent. You must call remov ...

  6. 关于myeclipse启动报错:An internal error has occurred. java.lang.NullPointerException解决办法

    启动myeclipse报错,百度了一下网友处理方式,对比日志,发现现在已有的教程真的是巨人坑: 如果出现了上述的错误按照如下的3个步骤解决:1.首先关闭MyEclipse工作空间.2.然后删除工作空间 ...

  7. 开启bin-log日志mysql报错:This function has none of DETERMINISTIC, NO SQL解决办法

    开启bin-log日志mysql报错:This function has none of DETERMINISTIC, NO SQL解决办法: 创建存储过程时 出错信息: ERROR 1418 (HY ...

  8. Eclipse及Eclipse为基础的App报错“Failed to create the Java Virtual Machine”的解决办法

    由于OracleJDK马上就要收费了,公司要求更换OpenJDK,结果安装后Eclipse及Eclipse为基础的App启动报错:“Failed to create the Java Virtual ...

  9. VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

    正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of ...

随机推荐

  1. macvim/babun+spf13-vim

    mac: macvim brew install macvim --with-cscope --with-lua --with-override-system-vim brew linkapps ma ...

  2. GIT 应用gitreview方式提交代码过程

    t status -- 是不是修改的文件 git diff (文件名) -- 看文件修改位置 git add (文件名的空格串) git commit -- 提交到本地 git stash -- 暂存 ...

  3. ebs R12 支持IE11

    如果不考虑升级jdk到1.7的话,可以使用oracle推荐的方法. 1.兼容模式 2.Toolkit to Disable Automatic Delivery of Internet Explore ...

  4. shell_script_查询主机名、ip地址 、DNS地址

    #!/bin/bashhostnameip=`/sbin/ifconfig eth0|grep "inet addr:"|sed 's/Bcast.*$//'g |awk -F & ...

  5. 20165210 Java第一次实验报告

    20165210 第一次实验报告 实验内容 建立目录运行简单的Java程序 建立自己学号的目录 在上个目录下建立src,bin等目录 Javac,Java的执行在学号目录下 IDEA的调试与设置断点 ...

  6. web 压力测试工具ab压力测试详解

    Web性能压力测试工具之ApacheBench(ab)详解 原文:http://www.ha97.com/4617.html PS:网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压 ...

  7. Windows之IOCP

    IOCP全称I/O Completion Port,中文译为I/O完成端口.IOCP是一个异步I/O的Windows API,它可以高效地将I/O事件通知给应用程序,类似于Linux中的Epoll,关 ...

  8. 统计数字noip2007

    7909:统计数字 总时间限制:  1000ms 内存限制:  65536kB 描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109).已知不相同的数不超过1000 ...

  9. QT QString与char *之间的转换 【转载】

    原文网址:http://blog.csdn.net/candyliuxj/article/details/6429208 1.QString转char * 先将QString转换为QByteArray ...

  10. 使用while 打印10~1,1~10

    使用while 打印10~1,1~10 #!/bin/bash i= ));do echo $i ((i--)) done 答案:109876543210 i= ));do echo $i ((i++ ...