Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Serialize Object to Database for Hibernate to read in Java

I'm currently writing a tool to plug into an existing enterprise application that uses Hibernate. My tool at install time needs to write some values into the database where one of the columns is a serialized version of a setting descriptor object. This object has two lists of objects and a few primitive types.

My current approach is to create a ByteArrayOutputStream and an ObjectOutputStream and then write the ObjectOutputStream to the ByteArrayOutputStream, then passing the resulting byte array into the sql with Spring's 1SimpleJdbcTemplate1. My current issue with this approach is that when the enterprise tool pulls my rows it fails to de-serialze the column with the following:

org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize

I feel I may need to serialize the inner objects, but have no clue how to do that and keep everything together.

like image 869
Patrick McDaniel Avatar asked May 11 '11 14:05

Patrick McDaniel


People also ask

How do you serialize an object in Java?

For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object.

Does Hibernate use serialization?

Typical Hibernate usage doesn't involve Serializable.


1 Answers

Ended up solving my own problem. In the hibernate API there is a class called SerializationHelper that has a static function serialize(Serializable obj) which I was able to use to serialize my object and then insert it into the database. Hibernate was then able to read it in the enterprise app.

like image 119
Patrick McDaniel Avatar answered Sep 27 '22 17:09

Patrick McDaniel