JDO requires that implementations support the persistence of java.lang.Object as first class objects
(FCO's). DataNucleus provides this capability and also provides that java.lang.Object can be stored as serialised.
It follows the same general process as for Interfaces since both interfaces
and java.lang.Object are basically
references
to some persistable object.
Let's suppose you have a field in a class and you have a selection of possible persistable class that
could be stored there, so you decide to make the field a
java.lang.Object
.
So let's take an example. We have the following class
public class ParkingSpace
{
String location;
Object occupier;
}
So we have a space in a car park, and in that space we have an occupier of the space. We have some
legacy data and so can't make the type of this "occupier" an interface type, so we just use
java.lang.Object
. Now we know that we can only have particular types of objects stored there
(since there are only a few types of vehicle that can enter the car park).
So we define our MetaData like this
<package name="org.com.mydomain.samples.object">
<class name="ParkingSpace">
<field name="location"/>
<field name="occupier" persistence-modifier="persistent"
field-type="com.mydomain.samples.vehicles.Car,
com.mydomain.samples.vehicles.Motorbike"/>
</field>
</class>
This will result in the following database schema.
So DataNucleus adds foreign keys from the ParkingSpace table to all of the possible implementation tables
for the
occupier
field.
In conclusion, for any java.lang.Object field in a class to be persisted (as non-serialised), you
must
define the possible "implementation" classes that can be stored there.
You can have a Collection/Map containing elements of java.lang.Object. You specify this in the same
way as you would any Collection/Map. DataNucleus supports having a Collection of references with multiple
implementation types as long as you use a join table relation.
By default a field of type
java.lang.Object
is stored as an instance of the underlying
PersistenceCapable in the table of that object. If either your Object field represents
non-PersistenceCapable objects or you simply wish to serialise the Object into the same table as
the owning object, you need to specify the "serialized" attribute, like this
<class name="MyClass">
<field name="myObject" serialized="true"/>
</class>
Similarly, where you have a collection of Objects using a join table, the objects are, by default,
stored in the table of the PersistenceCapable instance. If instead you want them to occupy a single
BLOB column of the join table, you should specify the "embedded-element" attribute of
<collection> like this
<class name="MyClass">
<field name="myCollection">
<collection element-type="java.lang.Object" serialized-element="true"/>
<join/>
</field>
</class>
Please refer to the serialised fields guide for more details
of storing objects in this way.