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
embedded
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
Computer
, and in our simple model our
Computer
has a graphics card and a sound card. So we model these cards using a
ComputerCard
class. So our classes become
public class Computer
{
private String operatingSystem;
private ComputerCard graphicsCard;
private ComputerCard soundCard;
public Computer(String osName,
ComputerCard graphics,
ComputerCard sound)
{
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,
int type)
{
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
Computer
objects into a table called
COMPUTER and we also want to persist the PC cards into the
same table
.
We define our MetaData like this
<entity name="mydomain.Computer">
<attributes>
<basic name="operatingSystem">
<column="OS_NAME"/>
</basic>
<embedded name="graphicsCard">
<attribute-override name="manufacturer">
<column="GRAPHICS_MANUFACTURER"/>
</attribute-override>
<attribute-override name="type">
<column="GRAPHICS_TYPE"/>
</attribute-override>
</embedded>
<embedded name="soundCard">
<attribute-override name="manufacturer">
<column="SOUND_MANUFACTURER"/>
</attribute-override>
<attribute-override name="type">
<column="SOUND_TYPE"/>
</attribute-override>
</embedded>
</attributes>
</entity>
<embeddable name="mydomain.ComputerCard">
<attributes>
<basic name="manufacturer"/>
<basic name="type"/>
</attributes>
</embeddable>
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
Computer
, they will be persisted into
this table.
It should be noted that in this latter (embedded) case we can still persist objects of
type
ComputerCard
into their own table - the MetaData definition for
ComputerCard
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 :-