Home Hibernate Mapping Collection of Basic Value Types - JPA with Hibernate

Mapping Collection of Basic Value Types – JPA with Hibernate

Understanding Collection Mapping of Basic Value types:

  • Collection mapping is mapping group of values to the single field or property in Entity. But we can’t store list of values in single table column in database.
  • See the following tables design User and Contacts. Here Contacts table having two columns ID and CONTACT_NO and both are primary keys.
  • Here ID and CONTACT_NO are owing by User table. Contacts table not having its own identity. ID column not primary key of Contacts table, it’s the the ID primary key value of User table.
  • And most important the relationship between User and Contacts table seems One-to-Many, but it’s not true because there is no foreign key relationship between these two tables.
Mapping Collection of basic value types

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

Java code for mapping collection of Basic Value Types :

@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;
    
    @Embedded
    @AttributeOverride(name="streetAddress", [email protected](name="STREET_ADDRESS"))
    private ContactAddress address;
    
    @ElementCollection
    @CollectionTable(name="Contacts", [email protected](name="ID"))
    @Column(name="CONTACT_NO")
    private Collection<String> contacts;
    
    @Enumerated(value=EnumType.STRING)
    @Column(name="USER_TYPE")
    private UserType userType;

    // Setters and Getters
  • Observe line numbers 27,28,29 from above code, @ElementCollection and @CollectionTable(name=“Contacts”, [email protected](name=“ID”)) explains the mapping collection of basic value types.

@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.

@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.

Note:
You can read more about Hibernate Entity Types and Value Types in article Entity Types vs Value Types


Download Application – JPA-Collection-Basic.zip (18 KB)

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. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories