GSON 報錯HibernateProxy. Forgot to register a type adapter? 的解決辦法


使用Gson轉換hibernate對象遇到一個問題,當對象的Lazy加載的,就會出現上面的錯誤。處理方式摘抄自網上,留存一份以后自己看。

 

網上找到的解決辦法,首先自定義一個類繼承TypeAdapter:

 1 package cn.xjy.finance.loanapply.utils ;
 2 
 3 import java.io.IOException ;
 4 import org.hibernate.Hibernate ;
 5 import org.hibernate.proxy.HibernateProxy ;
 6 import com.google.gson.Gson ;
 7 import com.google.gson.TypeAdapter ;
 8 import com.google.gson.TypeAdapterFactory ;
 9 import com.google.gson.reflect.TypeToken ;
10 import com.google.gson.stream.JsonReader ;
11 import com.google.gson.stream.JsonWriter ;
12 
13 /** 
14  * This TypeAdapter unproxies Hibernate proxied objects, and serializes them 
15  * through the registered (or default) TypeAdapter of the base class. 
16  */
17 public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {
18     
19     public static final TypeAdapterFactory    FACTORY    = new TypeAdapterFactory() {
20         @Override
21         @SuppressWarnings("unchecked")
22         public <T> TypeAdapter<T> create(Gson gson,
23                 TypeToken<T> type) {
24             return (HibernateProxy.class
25                     .isAssignableFrom(type
26                             .getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(
27                     gson) : null) ;
28         }
29     } ;
30                                                     
31     private final Gson                        context ;
32     
33     private HibernateProxyTypeAdapter(Gson context) {
34         this.context = context ;
35     }
36     
37     @Override
38     public HibernateProxy read(JsonReader in) throws IOException {
39         throw new UnsupportedOperationException("Not supported") ;
40     }
41     
42     @SuppressWarnings({ "rawtypes", "unchecked" })
43     @Override
44     public void write(JsonWriter out, HibernateProxy value) throws IOException {
45         if (value == null) {
46             out.nullValue() ;
47             return ;
48         }
49         // Retrieve the original (not proxy) class
50         Class<?> baseType = Hibernate.getClass(value) ;
51         // Get the TypeAdapter of the original class, to delegate the
52         // serialization
53         TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType)) ;
54         // Get a filled instance of the original class
55         Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
56                 .getImplementation() ;
57         // Serialize the value
58         delegate.write(out, unproxiedValue) ;
59     }
60 }

 

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

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

這樣就解決了,出現這個錯誤的主要原因是,hibernate采取懶加載的方式查詢數據庫,也就是只有用到了才去查真正的數據,用不到的話只是返回一個代理對象,gson識別不了代理對象,所以沒法轉換.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM