pbhle
pbhle

Reputation: 2936

Issue while saving serialize java object to Db

I have Java class configuration which is serializable.

I want to create an instance of configuration which contains data of type string,Document (org.w3c.dom.Document) and save it into Db which is of type BLOB.

But when i am going to save it into the DB it's throwing exception :

java.io.NotSerializableException: org.w3c.tidy.DOMElementImpl

My configuration class is:

public class Configuration extends Tag implements Serializable{

    private Document doc = null ;
    private String checkpoint=null; 

}

I have used following code when saving configuration object to DB :

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);   
out.writeObject(configuration);
byte[] confBytes = bos.toByteArray(); 

I am first converting it to byte array and then saving.

can anyone help me to get out of this issue..

Upvotes: 0

Views: 694

Answers (2)

jdevelop
jdevelop

Reputation: 12296

you may want to bypass these serialization issues with Kryo

Upvotes: 0

Gray
Gray

Reputation: 116908

java.io.NotSerializableException: org.w3c.tidy.DOMElementImpl

This is saying that the DOMElementImpl class is not marked as Serializable. Even though your Configuration class implements Serializable, all of the fields in your class need to do so as well. I assume that Document is the field that is the problem. To quote from this serialization tutorial:

Notice that for a class to be serialized successfully, two conditions must be met:

  • The class must implement the java.io.Serializable interface.

  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

Looking at the DOMElementImpl class, it does implement Serializable. If you need to serialize this to a database then you will need to export it to another class before storing to the database.

Upvotes: 2

Related Questions