Skip to content

Instantly share code, notes, and snippets.

@bytestree
Created May 7, 2016 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytestree/c21d32d5c08399a17141b9d7979a94f3 to your computer and use it in GitHub Desktop.
Save bytestree/c21d32d5c08399a17141b9d7979a94f3 to your computer and use it in GitHub Desktop.
Hibernate and Spring Configuration for Generic DAO example
package com.bytestree.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value={"com.bytestree"})
public class AppConfiguration {
}
package com.bytestree.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfig {
@Autowired
private Environment env;
/**
* Initialize dataSource
* @return DataSource
*/
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty("datasource.driver"));
dataSource.setUrl(env.getRequiredProperty("datasource.url"));
dataSource.setUsername(env.getRequiredProperty("datasource.username"));
dataSource.setPassword(env.getRequiredProperty("datasource.password"));
return dataSource;
}
/**
* Initialize hibernate properties
* @return Properties
*/
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.jdbc.batch_size", env.getRequiredProperty("hibernate.batch.size"));
return properties;
}
/**
* Initialize SessionFactory
* @return LocalSessionFactoryBean
*/
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(getDataSource());
sessionFactory.setPackagesToScan(new String[] { "com.bytestree.model" });
sessionFactory.setHibernateProperties(getHibernateProperties());
return sessionFactory;
}
/**
* Initialize Transaction Manager
* @param sessionFactory
* @return HibernateTransactionManager
*/
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment