JPA Tutorial - JPA Embeddable Example








The following sections demonstrate how to embed an entity into another entity.

First we create a Embeddable entity by marking the class with @Embeddable annotation.

@Embeddable @Access(AccessType.FIELD)
public class Address {

Then we mark the Embeddable entity with @Embedded when adding it to its container class.

    
@Entity
public class Employee {
    @Id private int id;
    private String name;
    private long salary;
    
    @Embedded 
    private Address address;




Example

The following code is from PersonDaoImpl.java.

package com.java2s.common;

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

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test() {

    Employee emp = new Employee();
    emp.setName("name");
    emp.setSalary(12345);
    

    Address p = new Address();
    p.setCity("New York");

    emp.setAddress(p);
    em.persist(emp);
  }

  @PersistenceContext
  private EntityManager em;
}

The following code is from Employee.java.

package com.java2s.common;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {
    @Id private int id;
    private String name;
    private long salary;
    
    @Embedded 
    private Address address;
    
    public Employee() {}

    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public long getSalary() {
        return salary;
    }

    public void setSalary(long salary) {
        this.salary = salary;
    }

    public Address getAddress() {
        return address;
    }
    
    public void setAddress(Address address) {
        this.address = address; 
    }

    public String toString() {
        return "Employee id: " + getId() + " name: " + getName() +
               " salary: " + getSalary() + " address: " + getAddress();
    }
}

The following code is from Address.java.

package com.java2s.common;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable @Access(AccessType.FIELD)
public class Address {
    private String street; 
    private String city; 
    private String state;
    @Column(name="ZIP_CODE")
    private String zip;

    public String getStreet() {
        return street;
    }
    
    public void setStreet(String address) {
        this.street = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }
    public String toString() {
        return "Address street: " + getStreet() +
               ", city: " + getCity() +
               ", state: " + getState() +
               ", zip: " + getZip();
    }

}


Download Embeddable.zip

The following is the database dump.

Table Name: EMPLOYEE
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 0

    Column Name: CITY,
    Column Type: VARCHAR:
    Column Value: New York

    Column Name: STATE,
    Column Type: VARCHAR:
    Column Value: null

    Column Name: STREET,
    Column Type: VARCHAR:
    Column Value: null

    Column Name: ZIP_CODE,
    Column Type: VARCHAR:
    Column Value: null

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

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