问题
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:
- Verify all the super classes of ModuleNode have empty argument constructor Since the exception happened during deserialization.
- 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