📜 ⬆️ ⬇️

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



image

Continuation of the series. Previously: Java EE 6. JPA 2.0 Review, Part 1: Introduction . In this article, I continue to look at changes in the JPA.
')
Built-in collections

The ElementCollection introduced in JPA 2.0 allows you to set display policies for unusual 1.0 JPA types of collections: collections of embeddable objects or collections of “simple” types (Integer, String, etc.). Also, this component is used when defining relations with Map, in the role of which any kind of objects act, and in the role of value - embeddable or “simple” objects.

ElementCollection values ​​are always stored in separate tables, which are specified by the @CollectionTable annotation. CollectionTable assigns a table name and @JoinColumn or @JoinColumns in the case of a composite primary key.

Collections of embedded objects

To define a collection of embedded objects, use the ElementCollection. The declaring of an embedded display 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 having to define feedbacks and add Id.

The difference between the ElementCollection and OneToMany is that target objects cannot be selected, saved, merged directly, regardless of the parent object. There is no cascading policy, the state of the 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 the type of sample, and by default it is lazy.

The time has come for a small example:
  1. 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 <Address> phones; //:~ }
  2. 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 <Address> phones; //:~ }
  3. 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 <Address> phones; //:~ }
  4. 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 <Address> phones; //:~ }
  5. 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 <Address> phones; //:~ }
  6. 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 <Address> phones; //:~ }
  7. 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 <Address> phones; //:~ }
  8. 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 <Address> phones; //:~ }
  9. 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 <Address> phones; //:~ }
  10. 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 <Address> phones; //:~ }
  11. 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 <Address> phones; //:~ }
  12. 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 <Address> phones; //:~ }
  13. 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 <Address> phones; //:~ }
  14. 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 <Address> phones; //:~ }
  15. 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 <Address> phones; //:~ }
  16. 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 <Address> phones; //:~ }


  1. import javax.persistence.Column;
  2. import javax.persistence.Embeddable;
  3. @Embeddable
  4. public class Address {
  5. private String city;
  6. private String street;
  7. @Column (name = "ZIP_CODE" )
  8. private String zip;
  9. //: ~ Next come accessories
  10. }


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

Collections from 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 the same as OneToMany with the exception that the target object is simple, not an entity. The disadvantages of this approach are similar to those listed in the previous paragraph.

It's time for an example:
  1. import javax.persistence. *;
  2. import java.util. List ;
  3. @Entity
  4. public class Customer {
  5. @Id
  6. @Column (name = "CUSTOMER_ID" )
  7. private long id;
  8. private String name;
  9. @ElementCollection
  10. @CollectionTable (
  11. name = "CUST_ADDRESSES" ,
  12. joinColumns = @ JoinColumn (name = "OWNER_ID" )
  13. )
  14. @Column (name = "ADDRESS" )
  15. private List < String > address;
  16. //: ~ Next come accessories
  17. }


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

Source: https://habr.com/ru/post/88683/


All Articles