The JPA persistence strategy typically involves persisting the fields of any class into its
own table, and representing any relationships from the fields of that class across to other
tables. There are occasions when this is undesirable, maybe due to an existing datastore
schema, or because a more convenient datastore model is required. JPA allows the
persistence of fields as
typically into the same table as the "owning" class.
One important decision when defining objects of a type to be embedded into another type is
whether objects of that type will ever be persisted in their own right into their own table,
and have an identity. JPA provides a MetaData attribute that you can use to signal this.
In a typical 1-1 relationship between 2 classes, the 2 classes in the relationship are
persisted to their own table, and a foreign key is managed between them. With JPA and
DataNucleus you can persist the related entity object as embedded into the same table.
This results in a single table in the datastore rather than one for each of the 2 classes.
Let's take an example. We are modelling a
, and in our simple model our
has a graphics card and a sound card. So we model these cards using a
class. So our classes become
public class Computer
private String operatingSystem;
private ComputerCard graphicsCard;
private ComputerCard soundCard;
public Computer(String osName,
this.operatingSystem = osName;
this.graphicsCard = graphics;
this.soundCard = sound;
public class ComputerCard
public static final int ISA_CARD = 0;
public static final int PCI_CARD = 1;
public static final int AGP_CARD = 2;
private String manufacturer;
private int type;
public ComputerCard(String manufacturer,
this.manufacturer = manufacturer;
this.type = type;
The traditional (default) way of persisting these classes would be to have a table to
represent each class. So our datastore will look like this
However we decide that we want to persist
objects into a table called
COMPUTER and we also want to persist the PC cards into the
We define our MetaData like this
So here we will end up with a TABLE called "COMPUTER" with columns "COMPUTER_ID",
"OS_NAME", "GRAPHICS_MANUFACTURER", "GRAPHICS_TYPE", "SOUND_MANUFACTURER", "SOUND_TYPE".
If we call persist() on any objects of type
, they will be persisted into
It should be noted that in this latter (embedded) case we can still persist objects of
into their own table - the MetaData definition for
is used for the table definition in this case.
DataNucleus supports embedded PC objects with the following proviso :-
Embedded PC objects cannot have inheritance (this restriction will hopefully be removed in the future,
allowing a discriminator).
See also :-