Hibernate Framework Basic

What is Hibernate

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 solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions.

Hibernate facilitates the storage and retrieval of Java domain objects via Object/Relational Mapping.

What Hibernate Does

  • Map Java class to database tables & vice versa
  • Data query and retrieval facility
  • Generate the SQL query based on the underline DB. and attempts to relieve the developer from manual result set handling and object conversion.
  • Make application portable to all relational DB.
  • Enhance performance by providing the different levels of cache(First, Second and Query level).

What is Dialect

Each SQL vendor has its own set of supported syntax. This is known as the dialect. In order to generate appropriate SQL query, Hibernate needs to know, for which DB query needs to be generated. Hibernate does it by org.hibernate.dialect.Dialect class and its subclass for each vendor.

E.g.:

DB2       :   org.hibernate.dialect.DB2Dialect

MySQL  :   org.hibernate.dialect.MySQLDialect

What is HQL

HQL is an abbreviation of Hibernate Query Language. It is SQL inspired language provided by hibernate. A developer can write SQL like queries to work with data objects.

Connection Pooling

Hibernate has its own internal connection pooling but it also supports some third-party connection pooling for production use.

  • c3p0 connection pool
  • Proxool connection pool
  • Obtaining connections from an application server, using JNDI

Hibernate Association

What is Hibernate Association: Any Table in DB could be connected to other tables in same or other DB. And these tables could be associated with each other via some keys(Foreign..). That kind of scenario can be handled by the association.

E.g: There is 2 table in DB, Student  & Subject. A student can study one subject or more than one subject. In that case, every student will have only one entry in Student table but Subject table could contain more than one record for the corresponding record in Student table.

Association mapping is used to map a Java Object to DB table.

Entities involved in mappings:

  • Class (Persistence)(POJO)
  • DB Table
  • Mapping file(.hbm)

Rules associated to POJO:

  • POJO class needs to have default constructor as it is required by Hibernate.
  • Lazy loading doesn’t work with the Final class. Its preferable to have Non-Final classes.

Type of Association

Unidirectional Association:

  • Many To One
  • One to One
  • One to Many

Unidirectional Association with join Table

  • Many To One
  • One to One
  • One to Many
  • Many to Many

Bidirectional Association

  • One to Many / Many to One
  • One to One

Bidirectional Association with join Table

  • One to Many / Many to One
  • One to One
  • Many to Many

Hibernate Caching

Caching is one way to improve the performance of the system, as it enables fewer queries going to the database. Querying the database is always a performance impact because it is an I/O operation. And I/O operations are much slower than operations using only the applications memory. There is two type of caching.

  • First Level Caching (Entity Cache)
  • Second Level Caching (Query Cache)

Click here for more details about Hibernate cache tutorial

In the next chapters, We will cover these topics in more details.

7 Comments Hibernate Framework Basic

  1. Samrat Ghosh

    Hi,
    I want to learn Hibernate for working on Enterprise Level application in a very short tym span.Can U plz mention what r the topic i must hav to know for cracking interview and working in a project comfortably.

    Thanks in Advnce,
    Samrat Ghosh 🙂

    Reply
  2. Ramanand

    Hi,
    I am using Hibernate implementation with JPA, while migrating server from Tomcat Openejb to jboss i am getting the below error without proper information in the log file.

    Error : org.hibernate.MappingException: component property not found: id

    please provide the more information on the issue why it’s coming

    Thanks in Advance,
    Ramanand

    Reply
  3. abhay

    Content are all fine..but ,try reducing the usage of hardcore programming terms ..u must try making contents and related technical terms as simple as possible….. 😉

    Reply
    1. Denis Duke

      I agree. It is not as easy as it seems to find, among all this stream on the network, an informative article about Hibernate. Today I read a good one, on the same topic,

      Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.