3.8.7.5. Embedded Mapping
JPA uses the AttributeOverride annotation to override the default mappings of an embeddable class.
The JPA Overview details this process in Embedded Mapping. AttributeOverrides suffice for simple mappings, but do not allow you to override complex mappings.
Also, JPA has no way to differentiate between a null embedded object and one with default values for all of its fields.
OpenJPA overcomes these shortcomings with the org.apache.openjpa.persistence.jdbc.EmbeddedMapping annotation. This annotation has the following properties:
-
String nullIndicatorColumnName: If the named column’s value isNULL, then the embedded object is assumed to be null. If the named column has a non-NULLvalue, then the embedded object will get loaded and populated with data from the other embedded fields. This property is entirely optional. By default, OpenJPA always assumes the embedded object is non-null, just as in standard JPA mapping.
If the column you name does not belong to any fields of the embedded object, OpenJPA will create a synthetic null-indicator column with this name.
In fact, you can specify a value of true to simply indicate that you want a synthetic null-indicator column, without having to come up with a name for it.
A value of false signals that you explicitly do not want a null-indicator column created for this mapping (in case you have configured your mapping defaults to create one by default).
* String nullIndicatorFieldName: Rather than name a null indicator column, you can name a field of the embedded type. OpenJPA will use the column of this field as the null-indicator column.
* MappingOverride[] overrides: This array allows you to override any mapping of the embedded object.
The EmbeddedMapping's overrides array serves the same purpose as standard JPA’s AttributeOverride s and AssociationOverride s.
In fact, you can also use the MappingOverride annotation on an entity class to override a complex mapping of its mapped superclass, just as you can with AttributeOverride and AssociationOverride s.
The MappingOverrides annotation, whose value is an array of MappingOverride s, allows you to override multiple mapped superclass mappings.
Each org.apache.openjpa.persistence.jdbc.MappingOverride annotation has the following properties:
-
String name: The name of the field that is being overridden. -
Column[] columns: Columns for the new field mapping. -
XJoinColumn[] joinColumns: Join columns for the new field mapping, if it is a relation field. -
ContainerTable containerTable: Table for the new collection or map field mapping. We cover collection mappings in Collections, and map mappings in Maps. -
ElementJoinColumn[] elementJoinColumns: Element join columns for the new collection or map field mapping. You will see how to use element join columns in Element Join Columns.
The following example defines an embeddable PathCoordinate class with a custom mapping of a java.awt.Point field to two columns.
It then defines an entity which embeds a PointCoordinate and overrides the default mapping for the point field.
The entity also declares that if the PathCoordinate's siteName field column is null, it means that no PathCoordinate is stored in the embedded record; the owning field will load as null.
import org.apache.openjpa.persistence.jdbc.*;
@Embeddable
public class PathCoordinate {
private String siteName;
@Persistent
@Strategy("com.xyz.openjpa.PointValueHandler")
private Point point;
...
}
@Entity
public class Path {
@Embedded
@EmbeddedMapping(nullIndicatorFieldName="siteName", overrides={
@MappingOverride(name="siteName", columns=@Column(name="START_SITE")),
@MappingOverride(name="point", columns={
@Column(name="START_X"),
@Column(name="START_Y")
})
})
private PathCoordinate start;
...
}