Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jdbc.driverClassName = com.mysql.jdbc.Driver
- //we can use our created DB name
- jdbc.url = jdbc:mysql://localhost:3307/DBName
- jdbc.username = root
- jdbc.password = PASSWORD
- hibernate.dialect = org.hibernate.dialect.MySQLDialect
- hibernate.show_sql = true
- hibernate.format_sql = true //first time we can use "create" then other using time we can use "update"
- hibernate.hbm2ddl.auto = create
- import org.hibernate.SessionFactory;
- 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.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.hibernate4.HibernateTransactionManager;
- import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import javax.sql.DataSource;
- import java.util.Properties;
- @Configuration
- @EnableTransactionManagement
- @ComponentScan("com.rcb.configurations")//this is my configuration source parth
- @PropertySource(value = { "classpath:application.properties" })//this is application propertie file map to config class
- public class HibernateConfiguration {
- @Autowired
- private Environment environment;
- @Bean
- public LocalSessionFactoryBean sessionFactory() {
- LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
- sessionFactory.setDataSource(dataSource());
- sessionFactory.setPackagesToScan(new String[] { "com.rcb.entities" });
- sessionFactory.setHibernateProperties(hibernateProperties());
- return sessionFactory;
- }
- private Properties hibernateProperties() {
- // TODO Auto-generated method stub
- Properties properties = new Properties();
- properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
- properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
- properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
- properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
- return properties;
- }
- @Bean
- public DataSource dataSource() {
- // TODO Auto-generated method stub
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
- dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
- dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
- dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
- return dataSource;//return the dataSource
- }
- @Bean
- @Autowired
- public HibernateTransactionManager transactionManger(SessionFactory sesion) {
- HibernateTransactionManager txtManager = new HibernateTransactionManager();
- txtManager.setSessionFactory(sesion);
- return txtManager;//return the TransactionManager
- }
- }
Add Comment
Please, Sign In to add comment