Guest User

Untitled

a guest
Nov 6th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. @Configuration
  2. @EnableTransactionManagement
  3. public class PersistenceJPAConfig{
  4.  
  5. @Bean
  6. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  7. LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  8. em.setDataSource(dataSource());
  9. em.setPackagesToScan(new String[] { "com.google.persistence.model" });
  10.  
  11. JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  12. em.setJpaVendorAdapter(vendorAdapter);
  13. em.setJpaProperties(additionalProperties());
  14.  
  15. return em;
  16. }
  17.  
  18. @Bean
  19. public DataSource dataSource(){
  20. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  21. dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
  22. dataSource.setUrl("jdbc:mysql://localhost:3306/spring_jpa");
  23. dataSource.setUsername( "user" );
  24. dataSource.setPassword( "password" );
  25. return dataSource;
  26. }
  27.  
  28. @Bean
  29. public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
  30. JpaTransactionManager transactionManager = new JpaTransactionManager();
  31. transactionManager.setEntityManagerFactory(emf);
  32.  
  33. return transactionManager;
  34. }
  35.  
  36. @Bean
  37. public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
  38. return new PersistenceExceptionTranslationPostProcessor();
  39. }
  40.  
  41. Properties additionalProperties() {
  42. Properties properties = new Properties();
  43. properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
  44. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  45. return properties;
  46. }
  47. }
Add Comment
Please, Sign In to add comment