Java Persistence/ElementCollection

=ElementCollection=

JPA 2.0 defines an  mapping. It is meant to handle several non-standard relationship mappings. An  can be used to define a one-to-many relationship to an   object, or a   value (such as a collection of  s). An  can also be used in combination with a    to define relationships where the key can be any type of object, and the value is an   object or a   value.

In JPA an  relationship is defined through the   annotation or the   element.

The  values are always stored in a separate table. The table is defined through the annotation or the   element. The  defines the table's   and   or   if a composite primary key.

Embedded Collections
An  mapping can be used to define a collection of   objects. This is not a typical usage of  objects as the objects are not embedded in the source object's table, but stored in a separate collection table. This is similar to a, except the target object is an   instead of an. This allows collections of simple objects to be easily defined, without requiring the simple objects to define an  or   inverse mapping. can also override the mappings, or table for their collection, so you can have multiple entities reference the same  class, but have each store their dependent objects in a separate table.

The limitations of using an  instead of a   is that the target objects cannot be queried, persisted, merged independently of their parent object. They are strictly privately owned (dependent) objects, the same as an  mapping. There is no  option on an , the target objects are always persisted, merged, removed with their parent. still can use a  type and defaults to   the same as other collection mappings.

Example of an ElementCollection relationship database
EMPLOYEE (table)

PHONE (table)

Basic Collections
An  mapping can be used to define a collection of   objects. The  values are stored in a separate collection table. This is similar to a, except the target is a   value instead of an. This allows collections of simple values to be easily defined, without requiring defining a class for the value.

There is no  option on an , the target objects are always persisted, merged, removed with their parent. still can use a  type and defaults to   the same as other collection mappings.

Example of an ElementCollection relationship to a basic value database
EMPLOYEE (table)

PHONE (table)

Primary keys in CollectionTable

 * The JPA 2.0 specification does not provide a way to define the  in the  . However, to delete or update an element of the   mapping, some unique key is normally required. Otherwise, on every update the JPA provider would need to delete everything from the   for the , and then insert the values back.  So, the JPA provider will most likely assume that the combination of all of the fields in the   are unique, in combination with the foreign key ( (s)).  This however could be inefficient, or just not feasible if the   is big, or complex.


 * Some JPA providers may allow the  to be specified in the , to resolve this issue.  Note in this case the   only needs to be unique for the collection, not the table, as the foreign key is included. Some may also allow the   option on the   to be used for this. Otherwise, if your   is complex, you may consider making it an   and use a   instead.

ElementCollection