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



    image

    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:
    1. import javax.persistence.*;
    2. import java.util.List;
    3.  
    4.  
    5. @Entity
    6. public class Customer {
    7.   @Id
    8.   @Column(name="CUSTOMER_ID")
    9.   private Long id;
    10.  
    11.   private String name;
    12.  
    13.   @ElementCollection
    14.   @CollectionTable(
    15.     name="CUST_ADDRESS",
    16.     joinColumns=@JoinColumn(name="OWNER_ID")
    17.   )
    18.   private List
      phones;
    19.  
    20.   //:~ Дальше идут акцессоры
    21. }


    1. import javax.persistence.Column;
    2. import javax.persistence.Embeddable;
    3.  
    4.  
    5. @Embeddable
    6. public class Address {
    7.  
    8.   private String city;
    9.  
    10.   private String street;
    11.  
    12.   @Column(name="ZIP_CODE")
    13.   private String zip;
    14.  
    15.   //:~ Дальше идут акцессоры
    16. }


    The output will be the following schema in the database:
    image

    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:
    1. import javax.persistence.*;
    2. import java.util.List;
    3.  
    4.  
    5. @Entity
    6. public class Customer {
    7.   @Id
    8.   @Column(name="CUSTOMER_ID")
    9.   private long id;
    10.  
    11.   private String name;
    12.  
    13.   @ElementCollection
    14.   @CollectionTable(
    15.     name="CUST_ADDRESSES",
    16.     joinColumns=@JoinColumn(name="OWNER_ID")
    17.   )
    18.   @Column(name="ADDRESS")
    19.   private List address;
    20.  
    21.  
    22.   //:~ Дальше идут акцессоры
    23.  
    24. }


    The output will be the following schema in the database:
    image

    Also popular now: