English 中文(简体)
ORM - Overview
  • 时间:2024-12-22

Hibernate - ORM Overview


Previous Page Next Page  

What is JDBC?

JDBC stands for Java Database Connectivity. It provides a set of Java API for accessing the relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL comppant database.

JDBC provides a flexible architecture to write a database independent apppcation that can run on different platforms and interact with different DBMS without any modification.

Pros and Cons of JDBC

Pros of JDBC Cons of JDBC

Clean and simple SQL processing

Good performance with large data

Very good for small apppcations

Simple syntax so easy to learn

Complex if it is used in large projects

Large programming overhead

No encapsulation

Hard to implement MVC concept

Query is DBMS specific

Why Object Relational Mapping (ORM)?

When we work with an object-oriented system, there is a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as Java or C# represent it as an interconnected graph of objects.

Consider the following Java Class with proper constructors and associated pubpc function −

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

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

Consider the above objects are to be stored and retrieved into the following RDBMS table −

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 problem, what if we need to modify the design of our database after having developed a few pages or our apppcation? Second, loading and storing objects in a relational database exposes us to the following five mismatch problems −

Sr.No. Mismatch & Description
1

Granularity

Sometimes you will have an object model, which has more classes than the number of corresponding tables in the database.

2

Inheritance

RDBMSs do not define anything similar to Inheritance, which is a natural paradigm in object-oriented programming languages.

3

Identity

An RDBMS defines exactly one notion of sameness : the primary key. Java, however, defines both object identity (a==b) and object equapty (a.equals(b)).

4

Associations

Object-oriented languages represent associations using object references whereas an RDBMS represents an association as a foreign key column.

5

Navigation

The ways you access objects in Java and in RDBMS are fundamentally different.

The Object-Relational Mapping (ORM) is the solution to handle all the above impedance mismatches.

What is ORM?

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C#, etc.

An ORM system has the following advantages over plain JDBC −

Sr.No. Advantages
1 Let’s business code access objects rather than DB tables.
2 Hides details of SQL queries from OO logic.
3 Based on JDBC under the hood.
4 No need to deal with the database implementation.
5 Entities based on business concepts rather than database structure.
6 Transaction management and automatic key generation.
7 Fast development of apppcation.

An ORM solution consists of the following four entities −

Sr.No. Solutions
1 An API to perform basic CRUD operations on objects of persistent classes.
2 A language or API to specify queries that refer to classes and properties of classes.
3 A configurable facipty for specifying mapping metadata.
4 A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions.

Java ORM Frameworks

There are several persistent frameworks and ORM options in Java. A persistent framework is an ORM service that stores and retrieves objects into a relational database.

    Enterprise JavaBeans Entity Beans

    Java Data Objects

    Castor

    TopLink

    Spring DAO

    Hibernate

    And many more

Advertisements