我有一个图表,我想存储在我的数据库中,并在另一个包中检索它。我的图形签名是:
public class ModuleToModuleDependencyGraph extends DependencyGraph<ModuleNode, ModuleToModuleDependency> implements Serializable
扩展和使用的类的签名:
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
我用来保存到数据库的代码(作为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);
从DB检索的代码
ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes("graph"));
ObjectInputStream ins = new ObjectInputStream(bais);
Object O = ins.readObject();
我已经在所有与图形相关的类中实现了Serializable(例如,边缘类)。虽然存储在DB中我没有问题但是当我尝试检索它时我得到了
java.io.InvalidClassException: com.mycompany.dpx.dependencytree.ModuleNode; class invalid for deserialization
为什么我收到此错误?我错过了让课程序列化的东西吗? 当我试图在没有使ModuleNode类实现Serializable的情况下存储在DB中时,它给了我一个错误,它无法序列化它,在实现接口之后我没有得到这样的错误,所以这意味着ModuleNode可以序列化吗?
更新:
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();
}
}
答案 0 :(得分:1)