hibernate结合使用gson转换json中一点看法

作者: bingyulei
发布时间:2015-07-08 18:37:57

转发请注明出处:http://www.cnblogs.com/shizhongtao/p/3680216.html

在前后台的交互中,经常需要把bean对象与xml或者json,这里就把自己以前遇到的问题做一下说明。

  • 如果bean中没有配置one-to-many或者many-to-many等映射关系,转化时候一般没有问题
  • 在配置了单项映射关系中,如果把(fetch = FetchType.LAZY)的话。当应用gson对其进行转换时候,会出现这个问题:
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?      at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:67)      at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:61)      at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)      at com.google.gson.internal.bind.ArrayTypeAdapter.write(ArrayTypeAdapter.java:93)      at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)      at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)      at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)      at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:107)      at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)      at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)      at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)      at com.google.gson.Gson.toJson(Gson.java:593)      at com.google.gson.Gson.toJson(Gson.java:572)      at com.google.gson.Gson.toJson(Gson.java:527)      at com.google.gson.Gson.toJson(Gson.java:507)      at com.test.Botest.test2(Botest.java:55)      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)      at java.lang.reflect.Method.invoke(Method.java:597)      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)      at org.junit.runners.ParentRunner.run(ParentRunner.java:300)      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
View Code

原因应该是在gson进行转换json字符串时候,得不到映射实体(除了id以外)属性。如果想解决这个问题,最简单的方法就是把FetchType改成EAGER。或者你可以为gson注册一个转换器。如下:

 1 Gson gson=new GsonBuilder().registerTypeAdapter(TXq.class, new JsonSerializer<TXq>() {   2             @Override   3             public JsonElement serialize(TXq src, Type typeOfSrc,   4                     JsonSerializationContext context) {   5                 JsonObject o=new JsonObject();   6                 o.addProperty("pid",  src.getPid());   7                 //o.addProperty("name", src.getName());   8                 return o;   9             }  10         }).create();

上面的类的转换器中注释的那句会引发hibernate查询数据库。

  • 如果你配置了映射的双向关联。那么使用json时候就会报错。因为他们互相引用,gson转换时候就会进入一个无穷的死循环中。这时候你可以利用注册转换器的方法(如上),或者把这个属性忽略掉。

小节:应该利用xmlstream等转化工具时候都会遇到这个问题。

标签: Hibernate JSON
来源:http://www.cnblogs.com/shizhongtao/p/3680216.html

推荐: