Java EE 6. JPA 2.0 Overview, Part 2: Collections

Continuation of the series. Previously: Java EE 6. JPA 2.0 Overview, Part 1: Introduction . In this article, I continue to review changes in JPA.
Inline Collections
The ElementCollection component introduced in JPA 2.0 allows you to set display policies for unusual types of collections in JPA 1.0: collections of embeddable objects, or collections of "simple" types (Integer, String, etc.). This component is also used in defining relations with Map, the key of which is any kind of object, and the value is embeddable or “simple” objects.
ElementCollection values are always stored in separate tables, which are specified by the @CollectionTable annotation. CollectionTable dispenses the table name and @JoinColumn or @JoinColumns in the case of a composite primary key.
Collections of embedded objects
An ElementCollection is used to define a collection of embedded objects. Declaring an embedded mapping is similar to OneToMany, except that the target table is Embeddable, not an entity. This makes it easier to declare collections of simple objects, without the need to define feedbacks and introduce Id.
The difference between ElementCollection and OneToMany is that the target objects cannot be selected, saved, or merged directly, regardless of the parent object. There is no cascading policy; the state of collection objects is synchronous with the state of the parent object. Well, in general, everything is the same as with embedded objects. However, the ElementCollection can be set to select the type, and by default it is lazy.
The time has come for a small example:
- import javax.persistence.*;
- import java.util.List;
-
-
- @Entity
- public class Customer {
- @Id
- @Column(name="CUSTOMER_ID")
- private Long id;
-
- private String name;
-
- @ElementCollection
- @CollectionTable(
- name="CUST_ADDRESS",
- joinColumns=@JoinColumn(name="OWNER_ID")
- )
- private List phones;
-
- //:~ Дальше идут акцессоры
- }
- import javax.persistence.Column;
- import javax.persistence.Embeddable;
-
-
- @Embeddable
- public class Address {
-
- private String city;
-
- private String street;
-
- @Column(name="ZIP_CODE")
- private String zip;
-
- //:~ Дальше идут акцессоры
- }
The output will be the following schema in the database:

Collections of objects of "simple" types
With simple collections, everything is also simple: an ElementCollection is used, a separate table is created for the values, everything is similar to OneToMany, except that the target object is simple, not an entity. The disadvantages of this approach are similar to those listed in the next paragraph.
It’s time for an example:
- import javax.persistence.*;
- import java.util.List;
-
-
- @Entity
- public class Customer {
- @Id
- @Column(name="CUSTOMER_ID")
- private long id;
-
- private String name;
-
- @ElementCollection
- @CollectionTable(
- name="CUST_ADDRESSES",
- joinColumns=@JoinColumn(name="OWNER_ID")
- )
- @Column(name="ADDRESS")
- private List
address; -
-
- //:~ Дальше идут акцессоры
-
- }
The output will be the following schema in the database:
