Hibernate embedded and embeddable entities

Posted By : Amit Mishra | 14-Jan-2021

In this post, we are going to talk about two different JPA annotations: @Embedded and @Embeddable. You can use @Embedded and @Embeddable whenever you want to have separate values common for more than one Entities. For example, you have an Employee and Staff Domain. In both domains, you want to add a few constraints and you don't want to repeat the code. You can define a separate domain for address and you can easily use that address with Staff Domain as well as Employee Domain.

 

@Embeddable is a composition of values. For example, we might have a Name class that is a composition of first-name and last-name, or an Address class that is a composition of street, city, postal code, etc. Which can be later on used in multiple Entities to be used as Embeddable Objects.

 

Let's Start with our Employee Class

package com.example.demo; 

import javax.persistence.Embedded; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
@Entity 
public class Employee {
 
     @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
     private Long id;
     private String name; 
     private String mobile; 
     @Embedded private Address employeeAddress; 

     //Standard setters and getters

}

The @Embedded annotation tells this to hibernate that it's embedded content and hibernate will include all the columns available in the class which we're embedding. Also, It will not create a separate table it will expand the current table.

 

Also Read: Reading a CSV file in java

 

Our Staff class

 
@Entity 
public class Staff { 
    
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 
    private String name; 
    private String department; 
    @Embedded private Address staffAddress; 

   //Standard setters and getters

}

 

The Embeddable Content  


@Embeddable 
public class Address { 

     private String addressLine1; 
     private String City; 
     private int zipCode; 

     //Standard setters and getters

}

 

Also Read: How To Integrate A 3rd Party API In Drupal

 

Test the code

 

@Test
public static void performOperation() {
   //Performing Employee save operation 
   Employee em = new Employee("Name", "mobile", new Address("line1", "city", 445121));
   erepo.save(em); 

   Employee saved = erepo.findByName("Name");

   Assert.equals(em, saved);
 }

 

The Application Logs

 

//Table creation Hibernate: 

drop table if exists employee CASCADE
Hibernate: drop table if exists staff CASCADE 
Hibernate: create table employee (id bigint generated by default as identity, city varchar(255), address_line1 varchar(255), zip_code integer not null, mobile varchar(255), name varchar(255), primary key (id)) 
Hibernate: create table staff (id bigint generated by default as identity, department varchar(255), name varchar(255), city varchar(255), address_line1 varchar(255), zip_code integer not null, primary key (id)) 

//Insert Data 
Hibernate: insert into employee (id, city, address_line1, zip_code, mobile, name) values (null, ?, ?, ?, ?, ?) 
Hibernate: select employee0_.id as id1_0_, employee0_.city as city2_0_, employee0_.address_line1 as address_3_0_, employee0_.zip_code as zip_code4_0_, employee0_.mobile as mobile5_0_, employee0_.name as name6_0_ from employee employee0_ where employee0_.name=? 

Thank you so much for giving your time to read this post.

 

Choose Oodles For SaaS App Development Services

 

We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at [email protected].

About Author

Author Image
Amit Mishra

Amit is a spring web developer. He has good knowledge of Spring Cloud, Spring Boot, Spring MVC, Hibernate, and some template engines like jsp and thymleaf.

Request for Proposal

Name is required

Comment is required

Sending message..