Hibernate Basics

Advantage of Hibernate over JDBC:
  1. Hibernate is data base independent, same code will work for all data bases like ORACLE,MySQL , SQLServer etc. 
  2. In case of JDBC query must be data base specific. 
  3. As Hibernate is set of Objects , you don't need to learn SQL language.
    You can treat TABLE as a Object .
    In case of JDBC you need to learn SQL.
  4.  Don't need Query tuning in case of Hibernate. If you use Criteria Quires in Hibernate then hibernate automatically tuned your query and return best result with performance.
    In case of JDBC you need to tune your queries.
  5. You will 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 .
    statistics about your query and database status.
    JDBC Not provides any statistics.
  6. Development fast in case of Hibernate because you don't need to write queries.  
  7. No need to create any connection pool in case of Hibernate. You can use c3p0.
    In case of JDBC you need to write your own connection pool.
  8. In the xml file you can see all the relations between tables in case of Hibernate. Easy readability.  
  9. You can load your objects on start up using lazy=false in case of Hibernate.
    JDBC Don't have such support.
  10. Hibernate Supports automatic versioning of rows but JDBC Not.  
Automatic versioning  technique:

          Automatic versioning is a simple technique for assuring data integrity.

          One of the optimistic locking features that Hibernate provides is automatic versioning. Versioned objects need to contain either a version or timestamp property that Hibernate uses to detect concurrent modifications at flush time. Hibernate’s versioning mechanism works by checking the number of rows that were affected by an update statement.
  1. Consider the following: clients A and B load the same record R. After a time A commits back R with data changed, and after a while B commits too .. changing data of R - but not from the state of R after commit A.
  2. Automatic versioning does the following: a field is used to version the row data. In the above scenario A and B obtain the record R with version t. After the commit A the version is changed, so when B tries to commit the check upon the version fails. 
Why do use lazy="false"?
It decides whether the child object is loading to the parent or not.
  1. We have to set it in the mapping file of the parent class.If lazy="true" (means not to load child)
  2.  By default the lazy loading is true.
  3.  But some cases we need to load the child object when the parent is loaded.
  4.  So in that case we'll have to write lazy="false".
Hibernate Query Types:
  1. Hibernate Query Language : 
    • Hibernate created a new language named Hibernate Query Language (HQL), the syntax is quite similar to database SQL language. 
    • The main difference between the two is that HQL uses class name instead of table name, and property names instead of column name.
  2. Hibernate Criteria Query :
    •  Hibernate Criteria API is a more object oriented and elegant alternative to Hibernate Query Language (HQL). It’s always a good solution to an application which has many optional search criteria.
    • The Criteria interface allows to create and execute object-oriented queries. 
    •  It is powerful alternative to the HQL but has own limitations. 
    •  Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective.
  3. Native Query :
    • Native query is purely SQL query.