JPA : Serialising Objects

JPA1 provides a way for users to specify that a field will be persisted serialised. This is of use, for example, to collections/maps/arrays which typically are stored using join tables or foreign-keys to other records. By specifying that a field is serialised a column will be added to store that field and the field will be serialised into it.

JPA's definition of serialising applies to any field and all in the same way, unlike the situation with JDO which provides much more flexibility. Perhaps the most important thing to bear in mind when deciding to serialise a field is that that object in the field being serialised must implement

Serialised Fields

Applicable to RDBMS, HBase, MongoDB

If you wish to serialise a particular field into a single column (in the table of the class), you need to simply mark the field as a "lob" (large object). Let's take an example. We have the following classes

and we want the animals collection to be serialised into a single column in the table storing the Farm class, so we define our MetaData like this

<entity class="Farm">
    <table name="FARM"/>
        <basic name="animals">
            <column name="ANIMALS"/>

So we make use of the lob element (or @Lob annotation). This specification results in a table like this

Provisos to bear in mind are

  • Queries cannot be performed on collections stored as serialised.

If the field that we want to serialise is of type String, byte[], char[], Byte[] or Character[] then the field will be serialised into a CLOB column rather than BLOB.

See also :-

Serialised Field to Local File

Applicable to RDBMS

Note this is not part of the JPA spec, but is available in DataNucleus to ease your usage. If you have a non-relation field that implements Serializable you have the option of serialising it into a file on the local disk. This could be useful where you have a large file and don't want to persist very large objects into your RDBMS. Obviously this will mean that the field is no longer queryable, but then if its a large file you likely don't care about that. So let's give an example

public class Person
    long id;

    @Extension(vendorName="datanucleus", key="serializeToFileLocation"
    AvatarImage image;

Or using XML

<entity class="Person">
        <basic name="image">
            <extension key="serializeToFileLocation" value="person_avatars"

So this will now persist a file into a folder person_avatars with filename as the String form of the identity of the owning object. In a real world example you likely will specify the extension value as an absolute path name, so you can place it anywhere in the local disk.