Skip to main content

Hibernate

The process of storing data to permanent place and retrieving data from permanent place is called as persistence and such data is called as persistent data.

Add, remove, read and modify operations on persistence data are called as persistence operations and the logic used for it is called as persistence logic.

The place where data will be stored permanently is called as persistence store. As the data became permanent we can use it any moment through programming.

Ex: Flat files, Database software etc...

Flat files: are normal files where data can stored and maintained by OS(Operating System). For example, .txt, .xls

Limitations of flat files:
  • No security for data.
  • Files may deleted by mistake.
  • Do not support query language like sql.
  • Dealing with huge amount of data is quite complex.
Object Database Software:
The database software that is capable of storing java objects or any other objects directly in table columns as values is called object database software.

Limitations of Object Database Software:
  • It stores and object containing multiple values in a column of a table. This is violation of normalization rule I which says don't store multiple values in a single column.
  • Generating reports from Object oriented database software is complex.
Drawbacks of implementing persistence logic by using JDBC:
  • JDBC uses SQL queries to implement persistence logic. These SQL queries are database dependent so JDBC based persistence logic becomes database dependent.
  • As JDBC based persistence logic is database dependent, change of database software becomes complex and disturbs persistence logic.
  • Programmer is responsible to take care of exception handling and transaction management.
  • When SQL select queries are executed JDBC code generates ResultSet object since ResultSet object is not a serializable object we cannot send this object over the network.
  • We need to write additional code to have connection pooling.
To overcome the above problems we can work with ORM [Object Relational Mapping] based persistence logic.

Object Relational Mapping:
  • The process of mapping java class with database table, java class member variables with database table columns and making java objects representing table rows having synchronization between them is called as OR Mapping.
  • In OR Mapping every table row will be represented by a separate java object.
  • Synchronization between java objects and table row means any modification in table row reflects in java object and vice verse.
  • To take care of Synchronization and OR Mapping based persistence logic development we need ORM software.
Advantages of ORM:
  • We can perform insert, delete, select and update operations on the table using java objects that are representing table rows.
  • Here database queries are not reuired. Hence OR Mapping based persistence logic becomes database independent persistence logic.
  • Change of database software from a running project becomes easy because there is no need of modifying persistence logic in OR Mapping.
  • As programmer developed java class objects are representing table rows we can make these objects as serializable by making the class to implement java.io.Serializable interface.
List of ORM software's:
  1. Hibernate [Soft Tree]
  2. iBatis [Apache]
  3. Toplink [Oracle Corp]
  4. Entity beans of EJB [Sun Microsystems]
  5. JPA(Java Persistence Api) [Sun Microsystems]
  6. OJB [Apache]
  7. JDO [Adobe]
Note: List has declared based on usage wide.

Hibernate: Hibernate is an open source, light weight ORM tool to develop DB independent persistence logic in java based enterprise application.

MVC [Model – View – Control]

M – Model – Business Logic + Persistence Logic
V - View – Persistence Logic
C - Controller – Integration Logic
we cannot use hibernate to develop persistence and business logic. It means to develop only persistence logic of an application.

API dependent: means classes and interface used in the application development must extend or implement one of the predefined classes or interfaces.
Ex: Our Servlet application is API dependent because of it must implement or extends from predefined GenericServelt or HttpServlet class.

Hibernate Application: are light weight because classes and interfaces are not API dependent . There is no need of container/web/application servers to manage hibernate applications.

POJO classes: The classes that are not API dependent are called POJO [Plain Old Java Object] classes. A POJO class is a class which will contains the private variables with getter and setter methods. In the below PojoEx.java file is the example of POJO class. This will helps you to map with database table.

PojoEx.java
package com.javatbrains;

import java.io.Serializable;

public class PojoEx implements Serializable {
    private int id;
    private int name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getName() {
        return name;
    }

    public void setName(int name) {
        this.name = name;
    }

}

POJI [Plaing Old Java Interface]: POJI is an interface which should not extend any predefined interfaces which belongs to API specific.

package com.javatbrains;

import java.io.Serializable;

public interface PojiEx {

}

interface iter1 extends PojiEx{}

interface inter2 extends Runnable{}

interface inter3 extends Serializable{}
The above interfaces are example of POJI because they have not extended from any of the API specific interfaces.

Hibernate API: Hibernate API contains multiple API. Mainly used API's are mentioned in the below.
  • Core API: Used by programmers to develop basic hibernate application.
  • Extension API: Programmer uses this API to add additional functionality/services to the application.
  • Miscellaneous API: This API provides built in tools related to hibernate.
  • Internal Implementation: Hibernate software and engine comes in the form of internal implementation API.

Comments

Popular posts from this blog

Multithreading in java with example

Multithreading  is one of the most important concept in core java. In this article we will learn what is multithreading? , what is the use of it? and What is the use of Synchronization and when to use it?  with detailed examples. At a time, two or more threads are accessing the same object is called as Multithreading  in Java .  First, we will create two threads for two objects. It is also possible to run two or more threads on a single class object. In this case, there is a possibility to get unreliable results. If the two threads are perform same task, then they need same object to be executed each time. For your better understanding, take an example of any reservations like, railway, movie ticket booking,etc. Let us think only one berth is available in a train and two passengers are asking for that berth. The first person has sent a request to allocate that ticket/berth to him. At the same time, the second person also sent a request to allocate that ...

Git installation for AngularJS 2 in Windows 10

Download Git latest version from https://git-scm.com/downloads or you click on the below link to download directly for windows https://git-scm.com/download/win . Once download completes, click on executable file to start installation process and choose Yes to allow the software installation in windows 10. Click on Next button to continue further installation. Browse the isntallation directory and click on Next button to continue. Select the list of components which you want to be installed and click on Next button to proced further installation. Type the shortcut name for Start menu and click on Next button. Select how you want to use the Git and click on Next button. For Windows no need to change anything, let it be the default one. Choose the Use the OpenSSL library and click on Next button. Select how should Git treat line ending in text files and click on Next button. Select which terminal emulator to use with Git and click on Next button. Configure extr...

JNDI configuration for Tomcat 9 with Oracle

In this article, I am going to place the required source code to get data from the table by using the JNDI configuration. Below are the environment details that I have configured currently. Windows - 7 Oracle - 10g Tomcat - 9 JDK - 8 Eclipse Oxygen Ojdbc6 jar required First, we need to create the Dynamic Web Project. If you don't know how to do <Click Here>. I have faced a lot of issues before getting the output like 405, No driver class to load, etc. I am using JSP & Servlets in the current explanation. Before started writing the application logic, we need to do the below configuration in the installed tomcat directory. Place OJDBC6.jar in the Tomcat LIB directory. Add this Resource under <GlobalNamingResources> in Server.xml file which is present under the conf directory in Tomcat. < Resource name = "jdbc/myoracle" global= "jdbc/myoracle" auth = "Container" type= "javax.sql.DataSource" driverClass...