How do I Serialize Object to Database for Hibernate to read in Java
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.
You can serealize a Java object into bytes and then store it in a BLOB.
Serialize:
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();ObjectOutputStream objOut = new ObjectOutputStream(byteOut);objOut.writeObject(object);objOut.close();byteOut.close();byte[] bytes = byteOut.toByteArray()
Deserialize:
public <T extends Serializable> T getObject(Class<T> type) throws IOException, ClassNotFoundException{ if(bytes == null){ return null; } ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes); ObjectInputStream in = new ObjectInputStream(byteIn); T obj = (T) in.readObject(); in.close(); return obj; }