Guest User

Untitled

a guest
Jul 19th, 2019
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Configuration
  2. @EnableTransactionManagement
  3. public class DatabaseConfig {
  4.  
  5. @Bean
  6. public LocalSessionFactoryBean sessionFactory() {
  7. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  8. sessionFactory.setDataSource(dataSource());
  9. sessionFactory.setPackagesToScan("com.tornikeshelia.model");
  10. sessionFactory.setHibernateProperties(getHibernateProperties());
  11. return sessionFactory;
  12. }
  13.  
  14. private Properties getHibernateProperties(){
  15. Properties prop = new Properties();
  16. Properties properties = new Properties();
  17. properties.setProperty("hibernate.hbm2ddl.auto", "update");
  18. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57InnoDBDialect");
  19. return properties;
  20. }
  21.  
  22. @Bean(name = "dataSource")
  23. public BasicDataSource dataSource(){
  24. BasicDataSource ds = new BasicDataSource();
  25. ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
  26.  
  27. ds.setUrl("jdbc:mysql://localhost:3306/test?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8");
  28. ds.setUsername("username");
  29. //ds.setPassword("");
  30.  
  31. return ds;
  32. }
  33.  
  34. @Bean
  35. @Autowired
  36. public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
  37. HibernateTransactionManager txManager = new HibernateTransactionManager();
  38. txManager.setSessionFactory(sessionFactory);
  39. return txManager;
  40. }
  41.  
  42. @Bean
  43. public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
  44. return new PersistenceExceptionTranslationPostProcessor();
  45. }
  46. }
RAW Paste Data