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. hibernate.properties – 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 -


@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:dbconfig.properties" })
@ComponentScan({ ApplicationConstants.MODEL_OBJ_LOCATION })
public class HibernateConfig {

@Autowired
private Environment env;

@Bean
public LocalSessionFactoryBean sessionFactory() {
        final LocalSessionFactoryBean sessionFactory = new         LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { ApplicationConstants.MODEL_OBJ_LOCATION});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}

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

dataSource.setInitialSize(Integer.parseInt(env
.getProperty(ApplicationConstants.DB_INITIAL_POOL_SIZE)));
dataSource.setMaxActive(Integer.parseInt(env
.getProperty(ApplicationConstants.DB_MAX_ACTIVE_POOL_SIZE)));
dataSource.setMaxIdle(Integer.parseInt(env
.getProperty(ApplicationConstants.DB_MAX_IDLE_POOL_SIZE)));
dataSource.setMinIdle(Integer.parseInt(env
.getProperty(ApplicationConstants.DB_MIN_IDLE_POOL_SIZE)));
dataSource.setDefaultAutoCommit(false);
return dataSource;
}

@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new    HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}

Properties hibernateProperties() {
return new Properties() {
/**
* Default serial version ID
*/
 private static final long serialVersionUID = 1L;
{
setProperty(ApplicationConstants.HIBERNATE_HBM2DDL,
env.getProperty(ApplicationConstants.HIBERNATE_HBM2DDL));
setProperty(ApplicationConstants.HIBERNATE_DIALECT,
env.getProperty(ApplicationConstants.HIBERNATE_DIALECT));
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({ "classpath:dbconfig.properties" }) : 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