I want to be able to store any Serializable object in a database against a unique ID. So I created a Java object that has a constructor taking those 2 values. The constructor converts the Serializable into bytes and then splits these into chunks of 4096 characters. These bytes are stored in a List in the object. So the object looks like:
public class MyObj {
private String id;
private List<byte[]> serData;
public MyObj(String id, Serializable value) {
this.id = id;
this.serData = convertSerializableToByteArray(value);
}
}
I want to store this into a database table but I want the database table to have 3 columns:
- ID
- line_no
- serialized_data (4096 bytes max)
The primary key of the table should be the ID and line_no combined. As an example, to store an object of ID ‘foo’ to a 10,000 character string, I would expect the database to have the following entries:
ID line_no serialized_data
-- ------- ----------------
foo 0 <first 4096 bytes>
foo 1 <4096 - 8192 bytes>
foo 2 <remaining 808 bytes>
I am having a lot of trouble creating either an hbm file for this or using Annotations. Can anyone help me out? Is this approach even possible? This is the hbm file I have so far (I am aware that this doesn’t work):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.foo.MyObject" table="settings_ser">
<list name="serializedData" cascade="all">
<key column="id" />
<index column="line_number"/>
<element type="binary" column="ser_data" length="4096" />
</list>
</class>
</hibernate-mapping>
Thanks