Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Hibernate ORM Overview


May 17, 2021 Hibernate


Table of contents


ORM overview

What is JDBC?

JDBC stands for Java Database Connectivity, a Java program that provides a set of Java APIs to access the relationship database. These Java APIs enable Java applications to execute SQL statements and interact with any SQL-compliant database.

JDBC provides a flexible framework for writing stand-alone applications that manipulate databases that can run on different platforms without modification and interact with different DBMS.

The advantages and disadvantages of JDBC

Advantages of JDBC The disadvantages of JDBC
Clean and tidy SQL handling The use of large projects is complex
Good performance under big data It's a big programming cost
Very good for small applications There is no encapsulation
Easy-to-learn simple grammar The concept of MVC is difficult to implement
The query needs to specify DBMS

Why Object Relationship Mapping (ORM)?

When we work in an object-oriented system, there is a mismatch between the object model and the relationship database. R DBMSs store data as tables, but object-oriented languages such as Java or C# represent an object association diagram. Consider the following Java classes with construction and public methods:

public class Employee {
   private int id;
   private String first_name; 
   private String last_name;   
   private int salary;  

   public Employee() {}
   public Employee(String fname, String lname, int salary) {
      this.first_name = fname;
      this.last_name = lname;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public String getFirstName() {
      return first_name;
   }
   public String getLastName() {
      return last_name;
   }
   public int getSalary() {
      return salary;
   }
}

Objects considered above need to be stored and indexed in the following RDBMS tables:

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

First question, what if we need to modify the design of the database after we have developed a few pages of code or applications?

Second, we face the following five mismatches when loading and storing objects in a relationship database.

Does not match Describe
Granularity Sometimes you will have an object model with more classes than the number of tables associated in the database
Inherited RDBMSs do not define any inheritance that is inherent in object-oriented programming languages
Identity RDBMS clearly defines the concept of 'sameness': primary keys. However, Java defines both object-judgment (a-b) and object value-judgment (a.equals(b))
Association Object-oriented programming languages use object references to represent associations, while an RDBMS uses foreign keys to represent object associations
Navigation Objects are accessed in Java and in RDBMS in exactly the same way

O bject-R Elational M Apping (ORM) is the solution to all of these mismatches.

What is ORM?

ORM, which represents Object-Relational Mapping (ORM), is a technology that makes it easy to transform data in relational databases and object-oriented programming languages such as Java, C, and so on. An ORM system has the following advantages over a normal JDBC.

Serial number Advantages
1 Use business code to access objects instead of tables in the database
2 Hide the details of SQL queries from object-oriented logic
3 JDBC-based 'under thehood'
4 There is no need to work with database implementations
5 Entities are business-based concepts, not database structures
6 Automatic generation of transaction management and keys
7 Rapid development of applications

An ORM solution consists of four entities:

Serial number Advantages
1 An API to implement basic CRUD operations on objects of persistent classes
2 A language or API to specify queries that refer to classes and properties
3 A configurable service is used to specify mapping metadata
4 A technology and transaction object interaction to perform dirty checking, lazy association fetching, and other optimized functions

Java ORM framework

There are several persistent frameworks and ORM options in Java. A persistent framework is the service that ORM stores and indexes objects to the relationship database.

  • Enterprise JavaBeans Entity Beans
  • Java Data Objects
  • Castor
  • TopLink
  • Spring DAO
  • Hibernate
  • And many more