I´m having a little problem and may be you can give a hand. What I´m trying to do is to persist any given java object (serializable) into a blob field in database.
The goal: Persist a java object in Oracle Blob
The way: Serializing the object and creating a BlobDomain as part of the entity, mapped to a BLOB field in database
The code:
The Entity:
@Entity
@Table(name = "ARGUMENT")
public class Argument implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id @Column(name = "ID")
private Long id;
@Column(name = "B_VALUE")
private BlobDomain value;
...
}
Setting the value:
private void createArgument (Serializable argVal) throws RuntimeException {
// argVal is the object to be stored
Argument arg = new Argument();
byte[] bytes = serialize(argVal);
BlobDomain value = new BlobDomain(bytes);
arg.setValue(value);
argumentDao.persist(arg);
}
public byte[] serialize(Object obj) throws IOException, SerialException, SQLException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bs);
os.writeObject(obj);
os.close();
return bs.toByteArray();
}
The error: The Argument entity is being properly sotred in database (ARGUMENT table), but when I try to query any row of the table, it throws the following exception:
org.hibernate.type.SerializationException: could not deserialize
Does anyone has an idea, why is this happening?
Thanks in advance!!
Full stack:
org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:188)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:211)
at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:105)
at org.hibernate.type.SerializableType.get(SerializableType.java:62)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2124)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
at org.hibernate.loader.Loader.getRow(Loader.java:1230)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
at org.hibernate.loader.Loader.doQuery(Loader.java:724)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2015)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:59)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:587)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1743)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:476)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:867)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:264)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:935)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
... 38 more
Caused by: java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:764)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:223)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:180)
... 67 more
You need to add annotation
Also you can define the length of colummn if object is big.
@Lob
, Specifies that a persistent property or field should be persisted as a large object to a database-supported large object type.