3.7.4.2.6. Type
OpenJPA has three levels of support for relations:
-
Relations that hold a reference to an object of a concrete persistent class are supported by storing the primary key values of the related instance in the database.
-
Relations that hold a reference to an object of an unknown persistent class are supported by storing the stringified identity value of the related instance. This level of support does not allow queries across the relation.
-
Relations that hold an unknown object or interface. The only way to support these relations is to serialize their value to the database. This does not allow you to query the field, and is not very efficient.
Clearly, when you declare a field’s type to be another persistence-capable class, OpenJPA uses level 1 support.
By default, OpenJPA assumes that any interface-typed fields you declare will be implemented only by other persistent classes, and assigns interfaces level 2 support.
The exception to this rule is the java.io.Serializable interface.
If you declare a field to be of type Serializable, OpenJPA lumps it together with java.lang.Object fields and other non-interface, unrecognized field types, which are all assigned level 3 support.
With OpenJPA’s type family of metadata extensions, you can control the level of support given to your unknown/interface-typed fields.
Setting the value of this extension to Entity indicates that the field value will always be some persistent object, and gives level 2 support.
Setting the value of this extension to the class of a concrete persistent type is even better; it gives you level 1 support (just as if you had declared your field to be of that type in the first place). Setting this extension to Object uses level 3 support.
This is useful when you have an interface relation that may not hold other persistent objects (recall that OpenJPA assumes interface fields will always hold persistent instances by default).
This extension is also used with OpenJPA’s externalization feature, described in Externalization.
OpenJPA defines the following type annotations for field values, collection, array, and map elements, and map keys, respectively: