使用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. AIM Tech Round 4 (Div. 2)ABCD

    这一场真的是血崩,a,b都被hack,还好结束前重交都过了 A:题意:找出得到k个不同的字符,所要更改的最小字符数 题解:首先如果k>字符串长度,直接impossible,然后直接记录一下不重复 ...

  2. 唯一id UUID

    public static String getUUID() { String s = UUID.randomUUID().toString(); return s.substring(0, 8) + ...

  3. Windows vs Linux:\r\n 与 \r

    Linux 下文本文件的换行符为 \n Windows 下文本文件的换行符为 \r\n,占两个字节: \r:归位键(CR),ascii 码为 13 \n:换行键(LF),ascii 码位 10 也即单 ...

  4. Git和Github使用说明

    1. 安装 官网地址:https://git-scm.com/downloads 我这里使用的是git version 2.19.1.windows.1,全程傻瓜式安装,点下一步即可,可以把命令模式和 ...

  5. Linux命令学习(17):ifconfig命令

    版权声明更新:2017-05-22博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 我们知道,在windows中,除了在图形界 ...

  6. HttpContext是干什么的

    这是MSDN对HttpContext的说明:        HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. (网上说是上下文信息,啥又叫上下文呢?个人感觉说的不 ...

  7. poj 2096 , zoj 3329 , hdu 4035 —— 期望DP

    题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...

  8. cpu高的问题的快速定位

    功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 58到家架构部,运维部,58速运技术部联合进行了一次线上 ...

  9. HDU1560(迭代加深搜索)

    DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. JQ选择器大全

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...