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 :-