×
DocsConsole

DocsConsole

ORM Introduction

By | | Updated : 2018-02-16 | Viewed : 514 times

Hibernate (ORM) Introduction

The current article explains about the RDBMS and JDBC technologies in brief. To overcome JDBC disadvantages, ORM tool, i.e., Hibernate has been introduced. So we will learn here the disadvantages of the JDBC here.

RDMS Introduction:

When we write Software for web application, we need to manage the persistence data. Persistence data is data, which will be preserved in Relational databases. Persistence data will be managed by RDMS (Relational Database Management Systems).

Relation Model organizes the data as table of columns and rows. It is based on Relational Model.

Relational Database Management Systems (RDMBS) are more useful to handle the data management. These RDBMS are not specific for particular language. RDMS is mainly used for providing the structured data/Relational data base, which is nothing but persistence data. With RDBMS we can execute SQL programming Language (i.e., DDL, DML).

    RDBMS Responsibilities:

  • Storing, organizing and retrieving the data.

  • Concurrency and data integrity.

  • Sharing the data between multiple users.

When we want to perform same above operations from Java technology then we should use JDBC (Java Data Base Connectivity).

JDBC (Java Database Connectivity):

JDBC is java based data base access technology, which can be used for accessing the database. JDBC is an Application Programmer Interface (API), which can be used for performing SQL CURD operations or to manage the persistence data. With JDBC API, we can execute CURD operations on persistence data.

Please find the below diagram for JDBC Work flow.

JDBC Working Flow

we will learn the JDBC by below given example.

JDBC example

Database Setup
-- Create database for docsconsole

CREATE DATABASE IF NOT EXISTS `docsconsole`;

Please create the table as given below and then insert the records as well.

Database Setup
--Create a table for employees

CREATE TABLE IF NOT EXISTS 'employees' (
  'emp_id' int(11) DEFAULT NULL,
  'emp_first_name' varchar(50) DEFAULT NULL,
  'emp_last_name' varchar(50) DEFAULT NULL
);

--Insert the Values into the table as given below

INSERT INTO 'employees' ('emp_id', 'emp_first_name', 'emp_last_name') VALUES
(101, 'Raj', 'Madav'), (102, 'Ram', 'Dev');


In creating the JDBC project, first we will create the java project as given below. Please follow below given steps in eclipse to create the Maven Java Project.

Create the Maven Project.
Click on File tab 
--> New 
--> Click on Maven Project 
--> Please check on Create Simple Project (Skip architype selection) 
--> Click on Next 
--> Enter the values for docsconsole.com.hibernate as Group Id, JDBCExample as Artifact Id 
--> Click on Finish

Note: Packaging should be jar.

Now we have created the Maven Java Project successfully.

Now we will create JDBCMainClient.java as given below.

Create JDBCMainClient.java
Right click on src/main/java 
--> New 
--> Click on Class 
--> Enter value for Package as docs.console.com and Enter value for Name JDBCMainClient.

Please write code for JDBCMainClient.java as given below.

JDBCMainClient.java
package docs.console.com;

import java.sql.*;

public class JDBCMainClient {

	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DB_URL = "jdbc:mysql://localhost/docsconsole";

	static final String USERNAME = "root";
	static final String PASSWORD = "";

	public static void main(String[] args) {

		System.out.println("::::::::::::::::--------------------Started main method here-------------------::::::::::::::::");

		Connection conn = null;
		Statement stmt = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");

			System.out.println("::::::::::::::::--------------------Connecting to database...");
			conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);

			System.out.println("::::::::::::::::--------------------Creating statement...");
			stmt = conn.createStatement();
			String sql;
			sql = "select emp_id, emp_first_name, emp_last_name FROM employees";
			ResultSet rs = stmt.executeQuery(sql);

			while (rs.next()) {

				int id = rs.getInt("emp_id");
				String firstName = rs.getString("emp_first_name");
				String lasName = rs.getString("emp_last_name");
				
				System.out.println("::::::::::::::::--------------------Result Row is started here-------------------::::::::::::::::");
				System.out.println("::::::::::::::::--------------------Employe Id:"+id);
				System.out.println("::::::::::::::::--------------------Employe First Name:"+firstName);
				System.out.println("::::::::::::::::--------------------Employe  last Name:"+lasName);
				System.out.println("::::::::::::::::--------------------Result Row is started here-------------------::::::::::::::::");
			}

			rs.close();
			stmt.close();
			conn.close();

		} catch (SQLException se) {

			se.printStackTrace();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {

			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException se2) {
			}
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException se) {
				se.printStackTrace();
			} 
		} 

	}
}

Add the dependencies for MySQL connector and pom.xml with below file.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>docsconsole.com.hibernate</groupId>
	<artifactId>JDBCApplication</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<mysql.connector.version>5.1.30</mysql.connector.version>
	</properties>
	
	<dependencies>
		<!-- MySql Driver -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.connector.version}</version>
		</dependency>
	</dependencies>
		
</project>

The created project architecture will be given below.

JDBC Exicution Flow

Once you run the above JDBCMainClient we will get result as given below.

JDBC Exicution Flow Result

Please find the below given observation in JDBCMainClient.java file related to the JDBC usage in the application.

    Code Observations are

  • Code is written for Driver registration, connection and statement creation. In short, more coding is required for query execution.

  • SQL queries are Database dependent. So if you want to perform different SQL operations in application, you will have to write the data base dependent queries.

  • After execution of the Query, all the connections should be closed by programing, which is more burden for the programmer.

  • Code enhancement will be more in the application after modifying the data base.

  • For all the operations, we should use the SQL queries, which is against to the object oriented programming.

  • Database table data Conversion into Java class object is bit costliest job with JDBC.

So many above problems can be resolved by Object Relation Mapping tools. First of all, what is Object Relation Mapping?

Object Relational Mapping:

Database table data can be mapped with Java class object by Object oriented programming is nothing but Object Relational Mapping. In other words, a technique used for mapping between Java object and Relational Database table is nothing but object relational Mapping. To implement ORM concepts, there are so many implemented Java libraries. One of the ORM tool is Hibernate. We will discuss here Hibernate Advantages over JDBC.

Hibernate advantages over JDBC

JDBC Hibernate
Queries are data base dependent HQL queries are database independent.
JDBC does not support for Lazy Loading Hibernate supports Lazy Loading
Developer need to write queries for Curd operations. Here we can use for Hibernate methods for CURD operations.
No support for Caching mechanism Supports Caching mechanism.
JDBC does not provide support for connection pool Hibernate provides support for connection pool
It will not support Identifier generation for primary key to insert the record It will support Identifier generation for primary key to insert the record
Caching mechanism cannot be used here. Caching mechanism can be used here
Leave A Reply