JPA doesn't specify support for persisting fields of type java.lang.Object, however DataNucleus does support this where the values of that field are persistable objects themselves. This follows the same general process as for Interfaces since both interfaces and java.lang.Object are basically references to some persistable object.
java.lang.Object cannot be used to persist non-persistable types with fixed schema datastore (e.g RDBMS). Think of how you would expect it to be stored if you think it ought to
DataNucleus allows the following ways of persisting Object fields :-
The user controls which one of these is to be used by specifying the extension mapping-strategy on the field containing the interface. The default is "per-implementation"
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
public class ParkingSpace { String location; @OneToOne @Extension(key="implementation-classes", value="mydomain.samples.vehicles.Car,mydomain.samples.vehicles.Motorbike") Object occupier; }
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, when using "per-implementation" mapping 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.
If we use mapping-strategy of "identity" then we get a different datastore schema.
public class ParkingSpace { String location; @OneToOne @Extensions({ @Extension(key="implementation-classes", value="mydomain.samples.vehicles.Car,mydomain.samples.vehicles.Motorbike"), @Extension(key="mapping-strategy", value="identity")}) Object occupier; }
and the datastore schema becomes
and the column "OCCUPIER" will contain strings such as com.mydomain.samples.object.Car:1 allowing retrieval of the related implementation object.
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 persistable in the table of that object. If either your Object field represents non-persistable objects or you simply wish to serialise the Object into the same table as the owning object, you need to specify it as "lob", like this
public class MyClass { @Lob Object myObject; }
Please refer to the serialised fields guide for more details of storing objects in this way.