JPA Part 1: Embedded Classes

Embedded classes are important because they are reusable via composition. For example in the following diagram, I have two Embeddable classes

  1. Contact
  2. Address

Here, Contact is embedded in the Employee, Address is embedded in the Contact. That means embeddable classes can embed other classes.

jpaex1

As shown in the above diagram, there is only one table is mapped to the entire class hierarchy.

Here the person class

package au.com.ojitha.blogspot.jpaex1.domain;  
  
import javax.persistence.Column;  
import javax.persistence.MappedSuperclass;  
  
/\*\*  
 \*  
 \* @author Ojitha  
 \*/  
@MappedSuperclass  
public class Person {  
      
    @Column(name = "FIRST\_NAME")  
    private String firstName;  
      
    @Column(name = "LAST\_NAME")  
    private String lastName;  
  
    public String getFirstName() {  
        return firstName;  
    }  
  
    public void setFirstName(String firstName) {  
        this.firstName = firstName;  
    }  
  
    public String getLastName() {  
        return lastName;  
    }  
  
    public void setLastName(String lastName) {  
        this.lastName = lastName;  
    }  
}  

Here the Employee Entity. Entity need a primary key. Here I used IDENTITY strategy. For more information about MySQL 5.6 Identity generation see the Part 2 of this series.

package au.com.ojitha.blogspot.jpaex1.domain;  
  
import javax.persistence.Column;  
import javax.persistence.Entity;  
import javax.persistence.Id;  
  
/\*\*  
 \*  
 \* @author Ojitha  
 \*/  
@Entity  
public class Employee extends Person{  
    @Id  
    @GeneratedValue(strategy=GenerationType.IDENTITY)  
    @Column(name = "EMP\_ID")  
    private int empId;  
  
    private Contact contact;  
      
    public int getEmpId() {  
        return empId;  
    }  
  
    public void setEmpId(int empId) {  
        this.empId = empId;  
    }  
  
    public Contact getContact() {  
        return contact;  
    }  
  
    public void setContact(Contact contact) {  
        this.contact = contact;  
    }  
   
}  

Here the Contact embeddable class

package au.com.ojitha.blogspot.jpaex1.domain;  
  
import java.io.Serializable;  
import javax.persistence.Embeddable;  
import javax.persistence.Embedded;  
  
/\*\*  
 \*  
 \* @author Ojitha  
 \*/  
@Embeddable  
public class Contact implements Serializable {  
    @Embedded  
    private Address address;  
  
    public Address getAddress() {  
        return address;  
    }  
  
    public void setAddress(Address address) {  
        this.address = address;  
    }  
      
      
}  

Here the Address embeddable class

package au.com.ojitha.blogspot.jpaex1.domain;  
  
import java.io.Serializable;  
import javax.persistence.Column;  
import javax.persistence.Embeddable;  
import javax.persistence.EnumType;  
import javax.persistence.Enumerated;  
  
/\*\*  
 \*  
 \* @author Ojitha  
 \*/  
@Embeddable  
public class Address implements Serializable{  
    @Column(name = "STREET")  
    private String street;  
      
    @Column(name = "SUBURB")  
    private String suburb;  
  
    @Enumerated(EnumType.STRING)  
    private State state;  
      
    public String getStreet() {  
        return street;  
    }  
  
    public State getState() {  
        return state;  
    }  
  
    public void setState(State state) {  
        this.state = state;  
    }  
      
    public void setStreet(String street) {  
        this.street = street;  
    }  
  
    public String getSuburb() {  
        return suburb;  
    }  
  
    public void setSuburb(String suburb) {  
        this.suburb = suburb;  
    }  
 }  

Here the state enumeration where character is selected for safe.

package au.com.ojitha.blogspot.jpaex1.domain;  
  
/\*\*  
 \*  
 \* @author Ojitha  
 \*/  
public enum State {  
    ACT, WA, NSW, VIC, SA  
}  

you can download the source from here.

Written with StackEdit.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python