hibernate snippet

ORM is an acronym for Object/Relational mapping. It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation, and data access.

The advantages of using ORM over JDBC

  • Application development is fast.
  • Management of transaction.
  • Generates key automatically.
  • Details of SQL queries are hidden.

Hibernate Architecture

  • Java application layer
  • Hibernate framework layer
  • Backhand api layer
  • Database layer

Hibernate Application Architecture:

JDBC (Java Database Connectivity)

JTA (Java Transaction API)

JNDI (Java Naming Directory Interface)

Elements of Hibernate Architecture

  • Configuration

    • Database Connection − This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
    • Class Mapping Setup − This component creates the connection between the Java classes and database tables.
  • SessionFactory

    SessionFactory is a thread-safe object, many threads cannot access it simultaneously.

  • Session

    • It maintains a connection between the hibernate application and database.It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
    • Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can share it between threads.
  • Transaction

  • ConnectionProvider

  • Query

    Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.

  • Criteria

    Criteria objects are used to create and execute object oriented criteria queries to retrieve objects.

About SQL

  • SQL query created in Hibernate(Native sql)


    //The method createSQLQuery() creates Query object using the native SQL syntax.
    Query query = session.createSQLQuery("Select * from Student");
  • HQL query


    //The method createQuery() creates Query object using the HQL syntax.
    Query query = session.createQuery("from Student s where s.name like 'k%'");
  • criteria query


    //The method createCriteria() creates Criteria object for setting the query parameters. This is more useful feature for those who don't want to write the query in hand. You can specify any type of complicated syntax using the Criteria API.
    Criteria criteria = session.createCriteria(Student.class);

HQL vs Criteria

  • HQL is to perform both select and non-select operations on the data, but Criteria is only for selecting the data, we cannot perform non-select operations using criteria
  • HQL is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries
  • HQL doesn’t support pagination concept, but we can achieve pagination with Criteria
  • Criteria used to take more time to execute then HQL
  • With Criteria we are safe with SQL Injection because of its dynamic query generation but in HQL as your queries are either fixed or parametrized, there is no safe from SQL Injection.


Cache memory stores recently used data items in order to reduce the number of database hits as much as possible.

  • First level Cache

    Hibernate first level cache is associated with the Session object. Hibernate first level cache is enabled by default and there is no way to disable it. However hibernate provides methods through which we can delete selected objects from the cache or clear the cache completely.Any object cached in a session will not be visible to other sessions and when the session is closed, all the cached objects will also be lost.

  • Second level Cache

    Hibernate Second Level cache is disabled by default but we can enable it through configuration. Currently EHCache and Infinispan provides implementation for Hibernate Second level cache and we can use them.

  • Query Cache

    Hibernate can also cache result set of a query. Hibernate Query Cache doesn’t cache the state of the actual entities in the cache; it caches only identifier values and results of value type. So it should always be used in conjunction with the second-level cache.

types of association mapping

  1. One to One

  2. One to Many

    (一对多)单向:会产生中间表,此时可以用@onetoMany @Joincolumn(name=” “)避免产生中间表**,并且指定了外键的名字(别看 @joincolumn在一中写着,但它存在在多的那个表中)

  3. Many to One

    (多对一)单向:不产生中间表,但可以用@Joincolumn(name=” “)来指定生成外键的名字,外键在多的一方表中产生!

  4. Many to Many