JPA Tutorial - JPA Embeddable Share Example








For an embedded entity we can share them among different entities.

We can also override the embeddable entity when embedding it.

The following code first creates an Address entity.

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

Then it creates a Company class and references the Embeddable entity.

@Entity
public class Company {
    @Id private int id;
    
    @Embedded
    private Address address;
    

After that it embedds the Address class again in Employee class. This time it overrides the attributes from the Address class.

@Entity
public class Employee {
  @Id private int id;
  private String name;
  private long salary;
  
  @Embedded 
  @AttributeOverrides({
      @AttributeOverride(name = "state", column = @Column(name = "PROVINCE")),
      @AttributeOverride(name = "zip", column = @Column(name = "POSTAL_CODE"))
  })
  private Address address;




Example

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();
    }

}

The following code is from Employee.java.

package com.java2s.common;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
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 
  @AttributeOverrides({
      @AttributeOverride(name = "state", column = @Column(name = "PROVINCE")),
      @AttributeOverride(name = "zip", column = @Column(name = "POSTAL_CODE"))
  })
  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 Company.java.

package com.java2s.common;


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


@Entity
public class Company {
    @Id private int id;
    
    @Embedded
    private Address address;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Address getAddress() {
        return this.address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }

    public String toString() {
        return "Company id: " + getId() + " address: " + getAddress();

    }
    
}

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);
    
    Company c = new Company();
    c.setAddress(p);
    c.setId(1);
    
    em.persist(emp);
    
    em.persist(c);
  }

  @PersistenceContext
  private EntityManager em;
}


Download Embeddable_Share.zip

The following is the database dump.

Table Name: COMPANY
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    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





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: PROVINCE,
    Column Type: VARCHAR:
    Column Value: null

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

    Column Name: POSTAL_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