1

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

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)
4

1 回答 1

1

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

当代码更改时 I 会更改的事实是一件好事,但在某些情况下它会导致您有些悲伤 :)

有关更多信息以及如何在必要时“修复”问题,请参见此处。

于 2013-01-02T21:51:58.547 回答