父/子序列化本地类不兼容异常

时间:2013-01-02 21:17:48

标签: java

我从以下两个类中获得了序列化兼容性错误。只有父类CommericalCustomer实现序列化。如下所示,在拥有父/子关系时使用Serializable接口的正确方法是什么?

public class CachedCommercialCustomers extends CommercialCustomer {


}

public class CommercialCustomer implements Serializable {

    private static final long serialVersionUID = 1L;

}

例外:

[#|2013-01-02T05:01:02.553-0800|SEVERE|glassfish3.1.2|com.hazelcast.nio.AbstractSerializer|_ThreadID=10;_ThreadName=Thread-2;|spot.api.model.vo.backoffice.CachedCommercialCustomers; local class incompatible: stream classdesc serialVersionUID = -2672531984245897526, local class serialVersionUID = -743225273062282831
java.io.InvalidClassException: com.sample.CachedCommercialCustomers; local class incompatible: stream classdesc serialVersionUID = -2672531984245897526, local class serialVersionUID = -743225273062282831
        at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:579)
        at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1600)
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1513)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1749)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
        at java.io.ObjectInputStream.readUnshared(ObjectInputStream.java:458)
        at com.hazelcast.nio.DefaultSerializer$ObjectSerializer.readNormal(DefaultSerializer.java:383)
        at com.hazelcast.nio.DefaultSerializer$ObjectSerializer.read(DefaultSerializer.java:353)
        at com.hazelcast.nio.DefaultSerializer.read(DefaultSerializer.java:134)
        at com.hazelcast.nio.CustomSerializerAdapter.read(CustomSerializerAdapter.java:33)
        at com.hazelcast.nio.AbstractSerializer.toObject(AbstractSerializer.java:116)
        at com.hazelcast.nio.AbstractSerializer.toObject(AbstractSerializer.java:146)
        at com.hazelcast.nio.Serializer.readObject(Serializer.java:72)
        at com.hazelcast.impl.ThreadContext.toObject(ThreadContext.java:103)
        at com.hazelcast.nio.IOUtil.toObject(IOUtil.java:149)
        at com.hazelcast.impl.BaseManager$RequestBasedCall.getResultAsObject(BaseManager.java:384)
        at com.hazelcast.impl.BaseManager$ResponseQueueCall.getResultAsObject(BaseManager.java:455)
        at com.hazelcast.impl.BaseManager$RequestBasedCall.getResultAsObject(BaseManager.java:368)
        at com.hazelcast.impl.BaseManager$ResponseQueueCall.getResultAsObject(BaseManager.java:455)

1 个答案:

答案 0 :(得分:1)

这里的问题是你没有为你的子类指定serialVersionId - 所以java会为你生成一个。{如果更改java源文件,则简单的重新编译可能会更改此值。请参阅spec

当代码改变时我会改变是一件好事,但在某些情况下会让你感到悲伤:)

有关详情以及如何在必要时“修复”问题,请参阅here