Hibernate

adplus-dvertising
Hibernate

Hibernate is a java package that makes it easy to work with relational database. Hibernate is an object-relational mapping (ORM) library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate provides a solution to map database tables to a class. It copies the database data to a class. In the other direction it supports to save objects to the database.

Learn Hibernate With Update Tutorial

Hibernate Different Examples

In this process the object is transformed to one or more tables. Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions. The primary feature of hibernate is mapping from java classes to database tables and from java data types to Sql data types.

Hibernate is a open source that provide a package to work with relational database. Hibernate is a library called Object Oriented Mapping (ORM) library. which provide a framework for mapping an object oriented model to traditional relational database.

Hibernate Tutorials and Interview Questions

The main feature of hibernate is mapping from java classes to database tables and similarly another feature is again mapping of java data types to the Sql data types Hibernate provide the data query and retrieval facilities which helps in development and reduce the development time. Hibernate can be used in java servlets ,swings based applications and J2EE applications along with EJB.

Hibernate generates the SQL calls and attempts to relieve the developer from manual result set handling and object conversion and keep the application portable to all supported SQL databases with little performance overhead. Two terms need to be understand in hibernate is Persistence and Mapping.

  • Persistence: Saving data to a storage is called persistence. Hibernate provides transparent persistence for Plain Old Java Objects (POJOs).

    persistent class is a no-argument constructor, not public.

    Persistence frame work: persistence frame work moves the data in its natural form to and from the permanent storage of data i.e. database

    Hibernate is a powerful, ultra-high performance object/relational persistence and query service for Java.

  • Mapping: Copying of tables to object and objects to table is called object relational mapping.

    using object relational mapping you can reuse your code. you can separate your dialog from your logic ,and logic from persistence mechanism.

    Mapping Java classes to database tables is accomplished through the configuration of an XML file or by using Java Annotations.

    Hibernate supports the mapping of custom value types. which makes the following scenario possible:

    • Mapping a single property to multiple columns
    • Mapping Java Enum to columns as if they were regular properties.
    • Overriding the default SQL type that Hibernate chooses when mapping a column to a property.
What Is Hibernate Query Language

Hibernate introduced a SQL inspired language called Hibernate Query Language (HQL). Which allows SQL-like queries to be written against Hibernate's data objects.

Hibernate Integrate Programming Language

Hibernate can be used both in standalone Java applications and in Java EE applications using servlets or EJB session beans. It can also be included as a feature in other programming languages.

History Of Hibernate

Hibernate was started in 2001 by Gavin King as an alternative to using EJB2-style entity beans. Its mission back then was to simply offer better persistence capabilities than offered by EJB2 by simplifying the complexities and allowing for missing features. Early in 2003, the Hibernate development team began Hibernate2 releases which offered many significant improvements over the first release and would go on to catapult Hibernate as the "de facto" standard for persistence in Java.

The Hibernate is Version 3.x. This version introduced new features like a new Interceptor/Callback architecture, user defined filters, and JDK 5.0 as of 2010 Hibernate 3 (version 3.5.0 and up) is a certified implementation of the Java Persistence API 2.0 specification via a wrapper for the Core module which provides conformity with the JSR.

The current version of Hibernate is Version 5.x

Hibernate API's

Hibernate application interface programming are present in the java package org.hibernate. Two interfaces are present in org.hibernate package

  1. .org.hibernate.SessionFactory interface: References immutable and threadsafe object creating new Hibernate sessions. Hibernate-based applications are usually designed to make use only of a single instance of the class implementing this interface.

  2. .org.hibernate.Session interface: Represents a Hibernate session i.e. the main point of the manipulation performed on the database entities. The latter activities include (among the other things) managing the persistence state (transient, persisted, detached) of the objects, fetching the persisted ones from the database and the management of the transaction demarcation.

Why Should one can use Hibernate

Hibernate provide important features of it that's why it is good to use hibernate.

  • Performance: Hibernate can save your time and efforts, and it can let you achieve some performance gains that you could hardly ever achieve by hand-coding.
  • Portable: Application portability is a key feature in Hibernate it allows developers to port applications to almost all SQL databases. data query and retrieval is possible with hibernate.
  • Set of objects: Hibernate is set of objects we don't need to learn SQL ,here we can treat table as object. In case of JDBC we need to learn SQL.
  • Fast development: Development fast in case of Hibernate because you don't need to write queries.
  • Get benefit of Cache: Hibernate support two level of cache. First level and 2nd level. So you can store your data into Cache for better performance. In case of JDBC you need to implement your java cache.
Architecture of Hibernate

To work with hibernate it is necessary to first create a java class which represent the table in a database. The next step is to map the instance variable in the class with the columns in the database. Now the hibernate is ready to perform the task on the database like insert, update ,delete etc. the records in the table. The query is automatically generated by the hibernate to perform these operations.

The above diagram shows that Hibernate is using the database and configure the data to provide persistence services to the application.

Hibernate architecture has three important components.

  1. Connection management: Connection management efficiently manage the data of data base. Data base connection is the very expensive part of interacting with the database as it requires a lot of resources of open and close the database connection.

  2. Transaction management: This management service provide the ability to the user to execute more than one database statements at a time.

  3. Object relational mapping: bject relational mapping is the technique of mapping the data from object model to the relational data model. This part of management is used to insert data ,update data ,select data, and delete the records from the table. In case of connection management and Transaction management hibernate lacks in performance and capabilities but it is very good tool as far as object relational mapping is concern.

How Hibernate Works

Hibernate provide tools to generate java classes from the description file. description files from existing data base and data base tables from description files.

Hibernate starts from simple java classes (Pojo = Plain Old Java Objects).

  1. To use hibernate you will first create a simple java class.

    public class Demo
    implements Serializable
    {
    private int hashValue = 0;
    private java.lang.Integer id;
    private java.lang.String title;
    public Demo()
    {
    }
    public Demo(java.lang.Integer fid) {
    this.setId(fid);
    }
    public java.lang.Integer getId() {
    return id;
    }
    public void setId(java.lang.Integer id) {
    this.id = id;
    }
    public java.lang.String getTitle() {
    return title;
    }
    public void setTitle(java.lang.String title) {
    this.title = title;
    }
    
  2. Now we create a mapping file. The mapping file explains Hibernate which field in the class is mapped to which field of the database.

    Mapping is used to insert ,update, select data to the table.

    <hibernate-mapping package="r4r">
    <class name="Demo" table="Emp">
    <id name="id" column="fid" type="java.lang.Integer">
    <generator class="sequence">
    <param name="sequence">public Demo_fid_seq</param>
    </generator>
    </id>
    <property name="title" column="ftitle" type="java.lang.String" />
    </class>
    </hibernate-mapping>
    
  3. Asclass is created in hibernate ,now we have start using the hibernate class. In this case classDemo and we did entry to the data base as:

    Demo demo = new Demo();
    demo.setTitle("Title");
    demo.setTuserFk(tuser);
    Transaction tx = session.beginTransaction();
    session.save(demo);
    tx.commit();
    

we can use MyEclipse to to create the description file. it provide functionality to create classes and description file from the data base directly.

Hibernate

Hibernate is a open source that provide a package to work with relational database. Hibernate is a library called Object Oriented Mapping (ORM) library. which provide a framework for mapping an object oriented model to traditional relational database.

The main feature of hibernate is mapping from java classes to database tables and similarly another feature is again mapping of java data types to the Sql data types Hibernate provide the data query and retrieval facilities which helps in development and reduce the development time. Hibernate can be used in java servlets ,swings based applications and J2EE applications along with EJB.

Hql

HQL is Hibernate Query Language it Sql like query language which allows to write queries against hibernate's data objects.

If HQL is used in the application then hibernate automatically generate the SQL query and execute it against the data base.

Features Of Hibernate
  • Hibernate support full object oriented query language.
  • Hibernate provides a solution to map database tables to a class this is called mapping.
  • Object relational mapping is one of the important feature of hibernate.
  • Hibernate provide a important feature of automatic key generation. i.e. automatic primary key generation.
  • Hibernate reduce the development time because it is totally follow the polymorphism ,inheritance and java collections.
Avantages of Hibernate
  • It takes less development time.
  • Hibernate is Robust and high in performance.
  • Hibernate is free under the LPGL lisence. so it can be used for develop application and distribute free.
  • Hibernate is database independent. it can be used for any database.