GWT:序列化异常

Posted

技术标签:

【中文标题】GWT:序列化异常【英文标题】:GWT : SerializationException 【发布时间】:2013-05-28 15:06:17 【问题描述】:

我正在尝试使用 GWT + Spring + Hibernate lunshing 应用程序时出现此错误:

com.google.gwt.user.client.rpc.SerializationException:类型“org.hibernate.collection.PersistentBag”未包含在可由此 SerializationPolicy 序列化的类型集中,或者无法加载其 Class 对象.出于安全考虑,此类型不会被序列化。:instance = [com.asso.shared.model.Activite@64d6357a]

将此方法与持久性类列表一起使用后:

public static <T> ArrayList<T> makeGWTSafe(List<T> list) 
        if(list instanceof ArrayList) 
            return (ArrayList<T>)list;
         else 
            ArrayList<T> newList = new ArrayList<T>();
            newList.addAll(list);
            return newList;
        
    

根据我的清单,我得到了这个:

com.google.gwt.user.client.rpc.SerializationException:类型“org.hibernate.collection.PersistentBag”未包含在可由此 SerializationPolicy 序列化的类型集中,或者无法加载其 Class 对象.出于安全考虑,此类型不会被序列化。: instance = [com.asso.shared.model.Personne@75a2fb58]

============================================

我已经搜索了其他主题,但找不到任何解决方案! 我该如何解决这个序列化的事情!? 我在我的 Persistence 类中使用 List

【问题讨论】:

【参考方案1】:

您需要将 DTO 对象发送到客户端(而不是由 Hibernate 支持的原始对象)。问题是您的 Personne 对象实际上是一个 Hibernate 代理。每次当你在它上面调用一些方法时,Hibernate 都会做一些工作(例如从数据库中获取集合)。序列化这类对象没有简单的方法。

休眠实体:

//Hibernate entity
public class Personne 

    private String name;
    private List<Address> addresses;


//Hibernate entity
public class Address 



对应的DTO对象:

public class PersonneDto 

    private String name;
    private List<AddressDto> addresses;


public class AddressDto 



您需要创建新的 PersonneDto 对象,将状态复制到它,然后发送到 UI,而不是将 Personne 发送到客户端。 Personne 不能在客户端使用,因为 Personne.getAddresses() 在大多数情况下会命中 DB 来获取数据(这在客户端 JS 中是不可能的)。所以每个Personne 必须在客户端替换为PersonneDto。不利的一面是,您需要维护额外的 DTO 对象层和相应的代码来将实体转换为 DTO。这个问题还有另一种方法。详情请见this article。

【讨论】:

如果可能的话,我需要一个非常清晰的解释和一个可运行的示例!如果你能做到,你应该知道你救了我,一个地球上某个地方的陌生人 PS: add(Personne p) 工作,但是当涉及到 get() 我有这个序列化错误 我不明白如何以及在何处使用 DTO 类!在服务器端,我正在操作 Personne 类,并且我与其他类有 ManyToMany 关系!我如何在这里使用 DTO this文章中有详细示例。对你来说更清楚了吗? 那么,使用 DTO 的方法对您有帮助吗?

以上是关于GWT:序列化异常的主要内容,如果未能解决你的问题,请参考以下文章

GWT RCP - 序列化异常

如何序列化 AutoBean (GWT) 中的列表?

GWT:枚举列表抛出“无法反序列化响应”

GWT JPA - 无法反序列化响应

解码根据 GWT 的序列化策略生成的 *.gwt.rpc 文件

如何使用 GWT 序列化流工厂