Search This Blog

Wednesday, May 14, 2014

Hibernate Configuration Using Annotation

Dear JTLs, In previous posts we learned about how to configure Spring MVC using annotations  and Initialization and configuration of Spring application using annotation. Today I will discuss about Hibernate configuration using annotation.
Hibernate ORM (Hibernate in short) is an object-relational mapping 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.
In previous hibernate releases we always tried to configure our hibernate app by using XML's. XML based configurations comes with following files -
  1. hibernate.cfg.xml – A standard XML file which contains hibernate configuration and which resides in root of application’s CLASSPATH
  2. – A Java compliant property file which holds key value pair for different hibernate configuration strings.
  3. resource.hbm.xml- For each database tables,  we need to create a mapping XML file which contains mapping of table columns with the XML column elements. After some time these XML based resource configuration are upgraded and we are now able to use Java classes with @Entity annotation to map table columns into our hibernate app entity classes.
So coming to the point what steps we need to configure hibernate using annotation, are given below in example class -

@PropertySource({ "" })
@ComponentScan({ ApplicationConstants.MODEL_OBJ_LOCATION })
public class HibernateConfig {

private Environment env;

public LocalSessionFactoryBean sessionFactory() {
        final LocalSessionFactoryBean sessionFactory = new         LocalSessionFactoryBean();
sessionFactory.setPackagesToScan(new String[] { ApplicationConstants.MODEL_OBJ_LOCATION});
return sessionFactory;

public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env .getProperty(ApplicationConstants.DB_DRIVER_CLASS_PROPERTY_NAME));
dataSource.setUsername(env .getProperty(ApplicationConstants.DB_USER_NAME_PROPERTY_NAME));
dataSource.setPassword(env .getProperty(ApplicationConstants.DB_PASSWORD_PROPERTY_NAME));

return dataSource;

public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new    HibernateTransactionManager();
return txManager;

public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();

Properties hibernateProperties() {
return new Properties() {
* Default serial version ID
 private static final long serialVersionUID = 1L;
setProperty(           ApplicationConstants.HIBERNATE_GLOBALY_QUOTED_IDENTIFIERS,
env.getProperty(ApplicationConstants.HIBERNATE_GLOBALY_QUOTED_IDENTIFIERS         ));

Using @Configuration Spring annotation we can declare any class as a configuration class the above class is configured to contain hibernate configuration.
@PropertySource({ "" }) : defines all the properties related to database like database URL, userName, password, connection pool etc will be available to this app 
.@ComponentScan({ ApplicationConstants.MODEL_OBJ_LOCATION }): Here component scan does scanning all the entity classes put into provided package.

This is all about hibernate configuration using annotation if you have any other option or way then please mention that into comment box.

No comments :

Post a Comment