Difference between revisions of "EclipseLink/Examples/JPA/2.0/ElementCollections"

From Eclipsepedia

Jump to: navigation, search
(Example of an ElementCollection relationship to a basic value database)
(How to map collections of Basic or Emeddable values using an ElementCollection mapping)
 
(2 intermediate revisions by one user not shown)
Line 2: Line 2:
 
JPA 2.0 defines an <code>ElementCollection</code> mapping.  It is meant to handle several non-standard relationship mappings.  An <code>ElementCollection</code> can be used to define a one-to-many relationship to an <code>Embeddable</code> object, or a <code>Basic</code> value (such as a collection of <code>String</code>s).  An <code>ElementCollection</code> can also be used in combination with a <code>Map</code> column to define relationships were the key can be any type of object, and the value is an <code>Embeddable</code> object or a <code>Basic</code> value.
 
JPA 2.0 defines an <code>ElementCollection</code> mapping.  It is meant to handle several non-standard relationship mappings.  An <code>ElementCollection</code> can be used to define a one-to-many relationship to an <code>Embeddable</code> object, or a <code>Basic</code> value (such as a collection of <code>String</code>s).  An <code>ElementCollection</code> can also be used in combination with a <code>Map</code> column to define relationships were the key can be any type of object, and the value is an <code>Embeddable</code> object or a <code>Basic</code> value.
  
In JPA a <code>ElementCollection</code> relationship is defined through the <code>[https://java.sun.com/javaee/6/docs/api/javax/persistence/ElementCollection.html @ElementCollection]</code> annotation or the <code><element-collection></code> element.
+
In JPA a <code>ElementCollection</code> relationship is defined through the <code>[http://www.eclipse.org/eclipselink/api/1.2/javax/persistence/ElementCollection.html @ElementCollection]</code> annotation or the <code><element-collection></code> element.
  
The <code>ElementCollection</code> values are always stored in a separate table.  The table is defined through the [https://java.sun.com/javaee/5/docs/api/javax/persistence/CollectionTable.html <code>@CollectionTable</code>] annotation or the <code><collection-table></code> element.  The <code>CollectionTable</code> defines the table's <code>name</code> and <code>[https://java.sun.com/javaee/5/docs/api/javax/persistence/JoinColumn.html @JoinColumn]</code> or <code>[https://java.sun.com/javaee/5/docs/api/javax/persistence/JoinColumns.html @JoinColumns]</code> if a composite primary key.
+
The <code>ElementCollection</code> values are always stored in a separate table.  The table is defined through the [http://www.eclipse.org/eclipselink/api/1.2/javax/persistence/CollectionTable.html <code>@CollectionTable</code>] annotation or the <code><collection-table></code> element.  The <code>CollectionTable</code> defines the table's <code>name</code> and <code>[http://www.eclipse.org/eclipselink/api/1.2/javax/persistence/JoinColumn.html @JoinColumn]</code> or <code>[http://www.eclipse.org/eclipselink/api/1.2/javax/persistence/JoinColumns.html @JoinColumns]</code> if a composite primary key.
  
 
==Emdedded Collections==
 
==Emdedded Collections==
Line 15: Line 15:
 
<table cellspacing=0 border=1 cellpadding=0>
 
<table cellspacing=0 border=1 cellpadding=0>
 
   <tr style="background:#ffdead">  <td>EMP_ID</td> <td>F_NAME</td> <td>L_NAME</td> <td>SALARY</td> </tr>
 
   <tr style="background:#ffdead">  <td>EMP_ID</td> <td>F_NAME</td> <td>L_NAME</td> <td>SALARY</td> </tr>
   <td>1</td> <td>Bob</td> <td>Way</td> <td>50000</td></tr>
+
   <tr><td>1</td> <td>Bob</td> <td>Way</td> <td>50000</td></tr>
   <td>2</td> <td>Joe</td> <td>Smith</td> <td>35000</td></tr>
+
   <tr><td>2</td> <td>Joe</td> <td>Smith</td> <td>35000</td></tr>
 
</table>
 
</table>
  

Latest revision as of 10:42, 30 November 2009

Contents

[edit] How to map collections of Basic or Emeddable values using an ElementCollection mapping

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

In JPA a ElementCollection relationship is defined through the @ElementCollection annotation or the <element-collection> element.

The ElementCollection values are always stored in a separate table. The table is defined through the @CollectionTable annotation or the <collection-table> element. The CollectionTable defines the table's name and @JoinColumn or @JoinColumns if a composite primary key.

[edit] Emdedded Collections

An ElementCollection mapping can be used to define a collection of Embeddable objects. This is not a typical usage of Embeddable 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 OneToMany, except the target object is an Embeddable instead of an Entity. This allows collections of simple objects to be easily defined, without requiring the simple objects to define an Id or ManyToOne inverse mapping. ElementCollection can also override the mappings, or table for their collection, so you can have multiple entities reference the same Embeddable class, but have each store their dependent objects in a separate table.

The limitations of using an ElementCollection instead of a OneToMany 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 Embedded mapping. Their is no cascade option on an ElementCollection, the target objects are always persisted, merged, removed with their parent. ElementCollection still can use a fetch type and defaults to LAZY the same as other collection mappings.

[edit] Example of an ElementCollection relationship database

EMPLOYEE (table)

EMP_ID F_NAME L_NAME SALARY
1 Bob Way 50000
2 Joe Smith 35000

PHONE (table)

OWNER_ID TYPE AREA_CODE P_NUMER
1 home 613 792-0001
1 work 613 494-1234
2 work 416 892-0005

[edit] Example of an ElementCollection relationship annotations

@Entity
public class Employee {
  @Id
  @Column(name="EMP_ID")
  private long id;
  ...
  @ElementCollection
  @CollectionTable(
        name="PHONE",
        joinColumns=@JoinColumn(name="OWNER_ID")
  )
  private List<Phone> phones;
  ...
}
@Embeddable
public class Phone {
  private String type;
  private String areaCode;
  @Column(name="P_NUMBER")
  private String number;
  ...
}

[edit] Example of an ElementCollection relationship XML

<entity name="Employee" class="org.acme.Employee" access="FIELD">
    <attributes>
        <id name="id">
            <column name="EMP_ID"/>
        </id>
        <element-collection name="phones">
            <collection-table name="PHONE">
                <join-column name="OWNER_ID"/>
            </collection-table>
        </element-collection>
    </attributes>
</entity>
<embeddable name="Phone" class="org.acme.Phone" access="FIELD">
    <attributes>
        <basic name="number">
            <column name="P_NUMBER"/>
        </basic>
    </attributes>
</embeddable>

[edit] Basic Collections

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

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

[edit] Example of an ElementCollection relationship to a basic value database

EMPLOYEE (table)

EMP_ID F_NAME L_NAME SALARY
1 Bob Way 50000
2 Joe Smith 35000

PHONE (table)

OWNER_ID PHONE_NUMBER
1 613-792-0001
1 613-494-1234
2 416-892-0005

[edit] Example of a ElementCollection relationship to a basic value annotations

@Entity
public class Employee {
  @Id
  @Column(name="EMP_ID")
  private long id;
  ...
  @ElementCollection
  @CollectionTable(
        name="PHONE",
        joinColumns=@JoinColumn(name="OWNER_ID")
  )
  @Column(name="PHONE_NUMBER")
  private List<String> phones;
  ...
}

[edit] Example of a ElementCollection relationship to a basic value XML

<entity name="Employee" class="org.acme.Employee" access="FIELD">
    <attributes>
        <id name="id">
            <column name="EMP_ID"/>
        </id>
        <element-collection name="phones">
            <column name="PHONE_NUMBER"/>
            <collection-table name="PHONE">
                <join-column name="OWNER_ID"/>
            </collection-table>
        </element-collection>
    </attributes>
</entity>