Serializable class cannot be deserialized

假装没事ソ 提交于 2021-01-28 07:27:14

问题


I have a graph i want to store in my DB and retrieve it in another package. My graph signature is:

public class ModuleToModuleDependencyGraph extends DependencyGraph<ModuleNode, ModuleToModuleDependency> implements Serializable

Signature of classes it extends and use :

public class DependencyGraph<V extends Node, E extends DependencyEdge<? extends DependencyData>> extends DefaultDirectedGraph<V, E> implements IDependencyGraph<V, E>, Serializable 
public class ModuleNode extends Node implements Serializable
public class ModuleToModuleDependency extends DependencyEdge<ModuleToModuleDependencyData> implements Serializable

The code i use to save to database(as BLOB)

ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
ObjectOutputStream oos1 = new ObjectOutputStream(bos1);
oos1.writeObject(moduleGraph);
oos1.flush();
oos1.close();
bos1.close();
byte[] graph = bos1.toByteArray();
statement = connectionDB.prepareStatement("INSERT IGNORE INTO " + TABLE_NAME + " VALUES (?)");
statement.setObject(1, graph);

Code to retrieve from DB

ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes("graph"));
ObjectInputStream ins = new ObjectInputStream(bais);
Object O = ins.readObject();

I have implemented Serializable in all classes which are related to the graph(edge class for example). While storing in the DB i have no problem but when i try to retrieve it I get

java.io.InvalidClassException: com.mycompany.dpx.dependencytree.ModuleNode; class invalid for deserialization

Why am i getting this error? Am i missing something to make the class serializable? When i tried to store in DB without making the ModuleNode class implement Serializable it gave me an error that it cannot serialize it, after implementing the interface i get no such error so it means that ModuleNode is serializable right?

UPDATE:

ModuleNode.java:

package com.mycompany.dpx.dependencytree;

import com.mycompany.dpx.dependencytree.graph.Node;

import java.io.Serializable;

public class ModuleNode extends Node implements Serializable{

    private static final long serialVersionUID = -1322322139926390329L;
    private String sdfClassName;
    private String moduleClassName;

    public ModuleNode(){
    }

    /**
     * Constructor for ModuleNode
     * @param uid Unique identifier for the module. Immutable.
     */
    public ModuleNode(String uid) {
        super(uid);
        this.sdfClassName = "";
        this.moduleClassName = "";
    }

    /**
     * Set the Source Data Factory class name for the module
     * @param sdfClassName
     */
    public void setSDFClassName(String sdfClassName) {
        this.sdfClassName = sdfClassName;
    }

    /**
     * Get the Source Data Factory class name for the module
     * @return sourceDataFactoryClassName
     */
    public String getSDFClassName() {
        return this.sdfClassName;
    }

    /**
     * Set the ViewModule class name for the module
     * @param moduleClassName
     */
    public void setModuleClassName(String moduleClassName) {
        this.moduleClassName = moduleClassName;
    }

    /**
     * Get the Module class name for the module
     * @return moduleClassName
     */
    public String getModuleClassName() {
        return this.moduleClassName;
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

}

回答1:


  1. Verify all the super classes of ModuleNode have empty argument constructor Since the exception happened during deserialization.
  2. Verify all the ModuleNode super classes implements serializable interface and their serialVersionIds are not changed during deserialization


来源:https://stackoverflow.com/questions/43783585/serializable-class-cannot-be-deserialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!