JPA Tutorial - JPA ElementCollection Generic TargetEntity Example

The following section shows how to map a Java Collection to database. It uses the @ElementCollection annotation to mark the element type in the Collection.


The following code is from

package com.java2s.common;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Employee {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    private long salary;

    // Using a targetClass instead of generics
    private Collection vacationBookings = new LinkedList();

    // Using generics in place of a targetClass
    private Set<String> nickNames = new HashSet();
    public Collection getVacationBookings() {
        return vacationBookings;

    public void setVacationBookings(Collection vacationBookings) {
        this.vacationBookings = vacationBookings;

    public Set<String> getNickNames() {
        return nickNames;

    public void setNickNames(Set<String> nickNames) {
        this.nickNames = nickNames;
    public int getId() {
        return id;
    public void setId(int id) { = id;
    public String getName() {
        return name;
    public void setName(String name) { = name;

    public long getSalary() {
        return salary;

    public void setSalary(long salary) {
        this.salary = salary;
    public String toString() {
        return "Employee id: " + getId() + " name: " + getName() + 
               " salary: " + getSalary() + 
               " nickNames: " + nickNames;

The following code is from

package com.java2s.common;

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

public class VacationEntry {
    private Calendar startDate;
    private int daysTaken;

    public VacationEntry() {}

    public VacationEntry(Calendar aStartDate, int daysTakenInt) {
        startDate = aStartDate;
        daysTaken = daysTakenInt;
    public Calendar getStartDate() {
        return startDate;
    public void setStartDate(Calendar startDate) {
        this.startDate = startDate;
    public int getDaysTaken() {
        return daysTaken;
    public void setDaysTaken(int daysTaken) {
        this.daysTaken = daysTaken;

    public String toString() {
        return "VacationEntry startDate: " + getStartDate().getTime() + 
               ", daysTaken: " + getDaysTaken();

The following code is from

package com.java2s.common;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

public class PersonDaoImpl {
  public void test() {

    Employee emp = new Employee();
    VacationEntry v = new VacationEntry();

  private EntityManager em;


Here is the database table dump.

Table Name: EMPLOYEE
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: name

    Column Name: SALARY,
    Column Type: BIGINT:
    Column Value: 12345

    Column Name: EMPLOYEE_ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NICKNAMES,
    Column Type: VARCHAR:
    Column Value: Tester

    Column Name: EMPLOYEE_ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: DAYS,
    Column Type: INTEGER:
    Column Value: 10

    Column Name: STARTDATE,
    Column Type: DATE:
    Column Value: null