Types of Object Relational Mapping in Hibernate JPA

  • By Dharamraj Pawale
  • November 25, 2023
  • JAVA Programming
Types of Object Relational Mapping in Hibernate JPA

Types of Object Relational Mapping in Hibernate JPA 

Explore Types of Object Relational Mapping in Hibernate JPA techniques and enhance your database interaction skills. Join now for in-depth learning at SevenMentor. Hibernate JPA is an ORM Framework written in Java. You may have a question What does ORM stand for? And What is it? Well to answer the first question, ORM stands for Object Relation Mapping.  Unleash the potential of Java, master coding intricacies, and open doors to exciting career opportunities with SevenMentor. Enroll now for an expert-led Java course in Pune.

It is a principle by which an object is represented as a database row. Alternatively, ORM is used to persist an object as a row in a relational database or alternatively, a row in a database is represented by an object in  Object Oriented Programming. 

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

Hibernate provides an implementation for ORM.  

Then you may question, what is JPA?  

Well, JPA stands for Java Persistence API. It is just a specification that facilitates Object Relation  Mapping to manage data in Java applications.  

Hibernate is the implementation of JPA specification. Hibernate is a JPA Provider means Hibernate follows the common standards provided by JPA. 

In Hibernate, java classes are mapped to the database table using mapping XML files or JPA  annotations.  

 Types of Object Relational Mapping in Hibernate JPA

Types of Object Relational Mapping in Hibernate JPA
Types of Object Relational Mapping in Hibernate JPA

A] Simple Class Object Relational Mapping

For example. 

Consider there is a Product Table in a relational database, with four columns such as Id, Name, Brand  and Price, in Hibernate Persistent class is created which is mapped to the database table as  below 

Table Structure is as below: 

Product Table 

Column Name  SQL Data Type
ID  NUMBER
NAME  VARCHAR
BRAND  VARCHAR
PRICE  FLOAT

 

Mapping class in Hibernate looks like as below: 

Product.java 

import javax.persistence.Column; 

import javax.persistence.Entity; 

import javax.persistence.GeneratedValue; 

import javax.persistence.Id; 

import javax.persistence.Table; 

@Entity 

@Table(name = “PRODUCT”) 

public class Product {

 

@Id 

@Column(name = “ID”) 

private int productId; 

@Column(name = “NAME”) 

private String name; 

private String brand; 

private float price; 

public int getProductId() { 

return productId; 

public void setProductId(int productId) { 

this.productId = productId; 

public String getName() { 

return name; 

public void setName(String name) { 

this.name = name; 

public String getBrand() { 

return brand; 

public void setBrand(String brand) { 

this.brand = brand; 

public float getPrice() { 

return price; 

public void setPrice(float price) { 

this.price = price; 

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

B] Component Object Relational Mapping

In this type of mapping, java containment meaning ‘HAS A’ relationship is mapped to the relational database table. Container objects and Contained objects are mapped to a single table. Fields of contained objects are mapped to the columns that are part of that table in which fields of  Container objects are mapped to some columns. Empower your Java journey with certification excellence. Join SevenMentor for top-tier Java Certification in Pune. Master Java programming, gain industry recognition and unlock new career heights. Enroll now for a brighter future!

 

The contained object class is annotated using JPA Annotation @Embeddable For example: Customer ‘HAS A’ Address 

Contained object class 

Address.java 

import javax.persistence.Column; 

import javax.persistence.Embeddable; 

@Embeddable 

public class Address { 

private int flatNo; 

@Column(name = “BUILDING”) 

private String buildingName; 

private String area; 

private String street; 

private String city; 

@Column(name = “PIN”) 

private int pinCode; 

public Address() { 

// TODO Auto-generated constructor stub 

public Address(int flatNo, String buildingName, String area, String street, String city, int  pinCode) { 

super(); 

this.flatNo = flatNo; 

this.buildingName = buildingName; 

this.area = area; 

this.street = street; 

this.city = city; 

this.pinCode = pinCode; 

public int getFlatNo() { 

return flatNo; 

public void setFlatNo(int flatNo) { 

this.flatNo = flatNo; 

public String getBuildingName() { 

return buildingName; 

public void setBuildingName(String buildingName) {

 

this.buildingName = buildingName; 

public String getArea() { 

return area; 

public void setArea(String area) { 

this.area = area; 

public String getStreet() { 

return street; 

public void setStreet(String street) { 

this.street = street; 

public String getCity() { 

return city; 

public void setCity(String city) { 

this.city = city; 

public int getPinCode() { 

return pinCode; 

public void setPinCode(int pinCode) { 

this.pinCode = pinCode; 

Container object class 

Customer.java 

import javax.persistence.Column; 

import javax.persistence.Embedded; 

import javax.persistence.Entity; 

import javax.persistence.Id; 

import javax.persistence.Table; 

@Entity 

@Table(name = “CUSTOMER_MASTER”) public class Customer {

 

@Id 

private int customerId; 

@Column(name = “C_NAME”) 

private String name; 

@Embedded 

private Address address; 

public Address getAddress() { 

return address; 

public void setAddress(Address address) { 

this.address = address; 

Please note that contained object property in container class Customer is annotated using JPA  Annotation @Embedded 

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

Types of Object Relational Mapping in Hibernate JPA

C] Inheritance Object Relational Mapping

In this type of mapping, the hierarchy of java classes (‘IS A’ KIND OF relationship) which are persistent  classes in Hibernate are mapped to relational database table/s 

There are 3 different types of Inheritance Mapping 

1] Table Per Class Hierarchy 

2] Table Per Concrete class 

3] Table Per Joined Subclass 

For inheritance mapping, JPA provides one common annotation @Inheritance with property strategy 

1] Table Per Class Hierarchy:  

In this type of inheritance mapping, all Java classes belonging to the hierarchy are mapped to a single database table. Inheritance Strategy SINGLE_TABLE is used and in the table, one extra column is  created discriminator column to distinguish between superclass and subclass entities persisted in  the table 

Example:  

Super Class – Employee 

Employee.java 

@Entity 

@Table(name = “EMP_MASTER”) 

@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 

@DiscriminatorColumn(name = “EMP_TYPE”,discriminatorType = DiscriminatorType.STRING) @DiscriminatorValue(“Emp”) 

public class Employee { 

@Id 

private int empId;

private String name; 

Sub class – Manager 

Manager.java 

@Entity 

@DiscriminatorValue(“MGR”) 

public class Manager extends Employee { 

private double incentives; 

2] Table Per Concrete Class: 

In this inheritance type mapping, the separate database table is created per entity class Super class objects are persisted in the superclass table, and sub-class objects are persisted in the subclass table. Inheritance Strategy TABLE_PER_CLASS is created. No need for a discriminator column. Its drawback is redundancy meaning the column in a super-class table must be repeated as it is in each sub-class table and no proper normalization.  

Example:  

Super class – Employee 

Employee.java 

@Entity 

@Table(name = “EMPLOYEE_M”) 

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 

public class Employee { 

@Id 

private int empId; 

private String name; 

Sub class – Manager 

Manager.java 

@Entity 

@Table(name = “MANAGER_MASTER”) 

public class Manager extends Employee { 

private double incentives; 

3] Table Per Joined Sub Class: 

In this third and last type of Inheritance Mapping, a separate table per class is created in a relational database. In this case, the Inheritance Strategy JOINED is used. The primary key column of the superclass acts as a Foreign key column in each subclass. No redundancy and proper normalization.

 

Do watch the video on Java: Click Here 

Example: 

Super class – Employee 

Employee.java 

@Entity 

@Table(name=”EMP”) 

@Inheritance(strategy = InheritanceType.JOINED) public class Employee { 

@Id 

private int empId; 

private String name; 

Subclass – Manager 

Manager.java 

@Entity 

@Table(name = “MGR_MASTER”) 

@PrimaryKeyJoinColumn(name = “MGR_ID”) public class Manager extends Employee { 

private double incentives; 

}

Author:-

Dharamraj Pawale

Call the Trainer and Book your free demo class for Java now!!!

© Copyright 2020 | SevenMentor Pvt Ltd

Submit Comment

Your email address will not be published. Required fields are marked *

*
*