Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.project.framework.configuration; import java.util.Properties; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; import static org.hibernate.cfg.Environment.*; import org.springframework.jdbc.datasource.DriverManagerDataSource; /** * * @author Rafael Benavides */ @Configuration @EnableTransactionManagement @PropertySource("classpath:db.properties") @ComponentScan(basePackages = { "com.project.framework.dao", "com.project.framework.service" }) public class DBConfig { @Autowired private Environment env; @Bean public LocalSessionFactoryBean getSessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(getDataSource()); sessionFactory.setHibernateProperties(getHibernateProperties()); sessionFactory.setPackagesToScan("com.project.framework.model"); return sessionFactory; } @Bean public DataSource getDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getProperty("postgresql.driver")); dataSource.setUrl(env.getProperty("postgresql.url")); dataSource.setUsername(env.getProperty("postgresql.username")); dataSource.setPassword(env.getProperty("postgresql.password")); return dataSource; } @Bean public Properties getHibernateProperties() { Properties properties = new Properties(); properties.put(DIALECT, env.getProperty("hibernate.dialect")); properties.put(USE_SQL_COMMENTS, env.getProperty("hibernate.use_sql_comments")); properties.put(SHOW_SQL, env.getProperty("hibernate.show_sql")); properties.put(FORMAT_SQL, env.getProperty("hibernate.format_sql")); properties.put(DEFAULT_SCHEMA, env.getProperty("hibernate.default_schema")); properties.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto")); properties.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size")); properties.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size")); properties.put(C3P0_ACQUIRE_INCREMENT, env.getProperty("hibernate.c3p0.acquire_increment")); properties.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout")); properties.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements")); return properties; } @Bean public HibernateTransactionManager getTransactionManager() { HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(getSessionFactory().getObject()); return transactionManager; } }