Home Hibernate Mapping Collection of Embeddable (Composite) Types

Mapping Collection of Embeddable (Composite) Types

This article dive into mapping of collection also known as mapping composite collections value type (Embeddable collection) with an example.

Database Tables Structure :

jpa hibernate collection embeddable user contact_address tables

  • Contact_Address table is Collection table.Contact_Address not owning its own identifier (Primary Key).
  • USER_ID not a primary key of Contact_Address, the value of USER_ID is primary key of USER Table.
  • There is no foreign key relation between USER and Contact_Address (do not confuse with one-to-many relation mapping, there is no foreign key relation ).
  • To map Contact_Address we have to create Composite value type (Embeddable), not an Entity Type.

Technologies Used in following example :

  • JPA 2.1
  • Hibernate 5.2.6
  • MySql 8.0
  • Maven 3
  • Spring Tool Suite (STS) 3.9.8
  • Java 1.8

ContactAddress.java :

@Embeddable
public class ContactAddress {
    
    @Column(name="STREET")
    private String streetAddress;
    
    @Column(name="STATE")
    private String state;
    
    @Column(name="CITY")
    private String city;
    
    @Column(name="ZIP_CODE")
    private String areaCode;
    
    @Column(name="ADDR_TYPE")
    private String addressType;
 
    // getters setters
    }

@Embeddable :

  • Defines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity. Each of the persistent properties or fields of the embedded object is mapped to the database table for the entity. It don’t have own identifier. In this tutorial ContactAddress is Embeddable.

@ElementCollection :

  • Defines a collection of instances of a basic type or embeddable class. Must be specified if the collection is to be mapped by means of a collection table. This annotation will be used to map collection basic value type or collection of Embeddable types.
  • @ElementCollection annotation is used to map either Basic Value type or Composite Value type.
  • At owning Entity(User) side not require to use @Embedded annotation, because Embeddable data stored in separate collection table.
  • Line numbers 23,24 from below code snippet describes the mapping the Collection of Embeddables.

@CollectionTable :

  • Specifies the table that is used for the mapping of collections of basic or embeddable types.Applied to the collection-valued field or property.

@JoinColumn :

  • In this Example @JoinColumn annotation used to map User entity Id  value(Primary key value) to the Contacts collection table’s ID column.
  • I will discuss more about @JoinColumn annotation in association mapping section.

Code Snippet for Mapping List of Embeddables :

- Advertisement -
@Entity(name="USER")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "ID")
    private Long id;
    
    @Column(name="USER_NAME")
    private String userName;
    
    @Column(name="PASSWORD")
    private String password;
    
    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name="CREATED_TIME")
    private Date creationTime;
    
    @Temporal(value=TemporalType.TIMESTAMP)
    @Column(name="UPDATED_TIME")
    private Date updatedTime;
    
    @ElementCollection
    @CollectionTable(name="CONTACT_ADDRESS", [email protected](name="USER_ID"))
    @AttributeOverride(name="streetAddress", [email protected](name="STREET_ADDRESS"))
    private List address;
    
    @ElementCollection
    @CollectionTable(name="Contacts", [email protected](name="ID"))
    @Column(name="CONTACT_NO")
    private Collection contacts;
    
    @Enumerated(value=EnumType.STRING)
    @Column(name="USER_TYPE")
    private UserType userType;

Note:
To understand Hibernate Composite(Embeddable) value types follw JPA Composite Mapping


Download Application – JPA-Collection-Composites.zip (14 KB)

- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles