Skip to content

Hibernate

Yash edited this page Nov 2, 2018 · 3 revisions

Hibernate is java based ORM tool that provides framework for mapping application domain objects to the relational database tables and vice versa.

Java Persistence API (JPA) provides specification for managing the relational data in applications. JPA specifications is defined with annotations in javax.persistence package. Using JPA annotation helps us in writing implementation independent code.

How to integrate Hibernate and Spring frameworks? Questions

Spring is one of the most used Java EE Framework and Hibernate is the most popular ORM framework. That’s why Spring Hibernate combination is used a lot in enterprise applications. The best part with using Spring is that it provides out-of-box integration support for Hibernate with Spring ORM module. Following steps are required to integrate Spring and Hibernate frameworks together.

  • Add hibernate-entitymanager, hibernate-core and spring-orm dependencies.
  • Create Model classes and corresponding DAO implementations for database operations. Note that DAO classes will use SessionFactory that will be injected by Spring Bean configuration.
  • If you are using Hibernate 3, you need to configure org.springframework.orm.hibernate3.LocalSessionFactoryBean or org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean in Spring Bean configuration file. For Hibernate 4, there is single class org.springframework.orm.hibernate4.LocalSessionFactoryBean that should be configured.

Note that we don’t need to use Hibernate Transaction Management, we can leave it to Spring declarative transaction management using @Transactional annotation.

Spring ORM provided two helper classes – HibernateDaoSupport and HibernateTemplate. The reason to use them was to get the Session from Hibernate and get the benefit of Spring transaction management.

Some of the design patterns used in Hibernate Framework are:

  • Domain Model Pattern – An object model of the domain that incorporates both behavior and data.
  • Data Mapper – A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.
  • Proxy Pattern for lazy loading
  • Factory pattern in SessionFactory

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.

An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database. (The ORM tool internally uses the JDBC API to interact with the database.)

Advantages of Hibernate Framework: « Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled bydefault.

Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don't need to write database specific queries. Before Hibernate, If database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.

The Dialect describes the SQL behaviour of the JDBC driver and database to which the application is connecting.

hibernate.dialect property makes Hibernate to generate the appropriate SQL statements for the chosen database.

List employees = session.createQuery("FROM Employee").list();

The HQL ("FROM Employee") gets converted to "SELECT * FROM EMPLOYEE" before hitting the MySQL database.

List of Hibernate SQL Dialects:

SQL dialect compatible with RDBMS org.hibernate.dialect.Dialect Subcalsses
HSQLDB (HyperSQL) org.hibernate.dialect.HSQLDialect
DB2 org.hibernate.dialect.DB2Dialect
DB2/400 org.hibernate.dialect.DB2400Dialect
DB2/390 org.hibernate.dialect.DB2390Dialect
MySQL (prior to 5.x) org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 9i/10g org.hibernate.dialect.Oracle9Dialect
DerbyDialect 10 org.hibernate.dialect.DerbyDialect
Derby 10.5 org.hibernate.dialect.DerbyTenFiveDialect
Derby 10.6 org.hibernate.dialect.DerbyTenSixDialect
Derby 10.7 org.hibernate.dialect.DerbyTenSevenDialect

« Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.

Clone this wiki locally