![]() | ![]() |
![]() |
| Project | Ver 1.1 | Ver 1.2 | JDO | JPA | Guides | Tools |
| 1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer |
JDO requires that implementations support the persistence of java.lang.Object as first class objects (FCO's). JPOX 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.jpox.samples.object">
<class name="ParkingSpace">
<field name="location"/>
<field name="occupier" persistence-modifier="persistent">
<extension vendor-name="jpox" key="implementation-classes"
value="org.jpox.samples.vehicles.Car,
org.jpox.samples.vehicles.Motorbike"/>
</field>
</class>This will result in the following database schema. ![]() So JPOX 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. In versions before 1.1.0-beta-5 JPOX didn't support having more than a single class implementing the specified reference. With JPOX 1.1.0-beta-5 onwards you can have 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. |