Guest User

Untitled

a guest
Oct 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. jdbc.driverClassName = com.mysql.jdbc.Driver
  2. //we can use our created DB name
  3. jdbc.url = jdbc:mysql://localhost:3307/DBName
  4. jdbc.username = root
  5. jdbc.password = PASSWORD
  6. hibernate.dialect = org.hibernate.dialect.MySQLDialect
  7. hibernate.show_sql = true
  8. hibernate.format_sql = true //first time we can use "create" then other using time we can use "update"
  9. hibernate.hbm2ddl.auto = create
  10.  
  11. import org.hibernate.SessionFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.ComponentScan;
  15. import org.springframework.context.annotation.Configuration;
  16. import org.springframework.context.annotation.PropertySource;
  17. import org.springframework.core.env.Environment;
  18. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  19. import org.springframework.orm.hibernate4.HibernateTransactionManager;
  20. import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  21. import org.springframework.transaction.annotation.EnableTransactionManagement;
  22.  
  23. import javax.sql.DataSource;
  24. import java.util.Properties;
  25.  
  26.  
  27.  
  28.  
  29. @Configuration
  30. @EnableTransactionManagement
  31. @ComponentScan("com.rcb.configurations")//this is my configuration source parth
  32. @PropertySource(value = { "classpath:application.properties" })//this is application propertie file map to config class
  33. public class HibernateConfiguration {
  34. @Autowired
  35. private Environment environment;
  36.  
  37. @Bean
  38. public LocalSessionFactoryBean sessionFactory() {
  39. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  40. sessionFactory.setDataSource(dataSource());
  41. sessionFactory.setPackagesToScan(new String[] { "com.rcb.entities" });
  42. sessionFactory.setHibernateProperties(hibernateProperties());
  43. return sessionFactory;
  44.  
  45. }
  46.  
  47. private Properties hibernateProperties() {
  48. // TODO Auto-generated method stub
  49. Properties properties = new Properties();
  50. properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
  51. properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
  52. properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
  53. properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
  54. return properties;
  55. }
  56.  
  57. @Bean
  58. public DataSource dataSource() {
  59. // TODO Auto-generated method stub
  60. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  61. dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
  62. dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
  63. dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
  64. dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
  65. return dataSource;//return the dataSource
  66. }
  67.  
  68. @Bean
  69. @Autowired
  70. public HibernateTransactionManager transactionManger(SessionFactory sesion) {
  71. HibernateTransactionManager txtManager = new HibernateTransactionManager();
  72. txtManager.setSessionFactory(sesion);
  73. return txtManager;//return the TransactionManager
  74.  
  75. }
  76. }
Add Comment
Please, Sign In to add comment