Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. package ph.com.alliance.bootstrap;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.persistence.EntityManagerFactory;
  6. import javax.sql.DataSource;
  7.  
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
  11. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  12. import org.springframework.orm.jpa.JpaTransactionManager;
  13. import org.springframework.orm.jpa.JpaVendorAdapter;
  14. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  15. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  16. import org.springframework.transaction.annotation.EnableTransactionManagement;
  17.  
  18. /**
  19. * Database configuration. Note the 'Import' annotation in RootConfig that activates this. PropertySource and
  20. * Environment pair allows externalized settings.
  21. *
  22. */
  23. @Configuration
  24. @EnableTransactionManagement
  25. public class DatabaseConfigMySQL{
  26.  
  27. @Bean
  28. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  29. LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  30. em.setDataSource(dataSource());
  31. em.setPackagesToScan(new String[] {"ph.com.alliance.entity"});
  32. JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  33. em.setJpaVendorAdapter(vendorAdapter);
  34. em.setJpaProperties(additionalProperties());
  35. return em;
  36. }
  37.  
  38. @Bean
  39. public DataSource dataSource(){
  40. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  41. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  42. dataSource.setUrl("jdbc:mysql://localhost:3306/kanbanwa");
  43. dataSource.setUsername("root");
  44. dataSource.setPassword("root");
  45.  
  46. return dataSource;
  47. }
  48.  
  49. @Bean
  50. public JpaTransactionManager transactionManager(EntityManagerFactory emf){
  51. JpaTransactionManager transactionManager = new JpaTransactionManager();
  52. transactionManager.setEntityManagerFactory(emf);
  53. return transactionManager;
  54. }
  55.  
  56. @Bean
  57. public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
  58. return new PersistenceExceptionTranslationPostProcessor();
  59. }
  60.  
  61. Properties additionalProperties() {
  62. Properties properties = new Properties();
  63. properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
  64. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  65. return properties;
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement