Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. package ...;
  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.PlatformTransactionManager;
  17. import org.springframework.transaction.annotation.EnableTransactionManagement;
  18.  
  19.  
  20. @Configuration
  21. @EnableTransactionManagement
  22. public class JPAConfiguration {
  23.  
  24. @Bean
  25. public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
  26. LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  27. em.setDataSource(dataSource);
  28. em.setPackagesToScan(new String[] { "caminho.meu" });
  29.  
  30.  
  31. JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  32. em.setJpaVendorAdapter(vendorAdapter);
  33. em.setJpaProperties(additionalProperties());
  34.  
  35. return em;
  36. }
  37.  
  38. @Bean(name = "dataSource")
  39. public DriverManagerDataSource dataSource() {
  40. DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
  41. driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
  42. driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/meudb");
  43. driverManagerDataSource.setUsername("ruti");
  44. driverManagerDataSource.setPassword("ruti");
  45. return driverManagerDataSource;
  46. }
  47.  
  48. @Bean
  49. public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
  50. JpaTransactionManager transactionManager = new JpaTransactionManager();
  51. transactionManager.setEntityManagerFactory(emf);
  52. return transactionManager;
  53. }
  54.  
  55. @Bean
  56. public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
  57. return new PersistenceExceptionTranslationPostProcessor();
  58. }
  59.  
  60. Properties additionalProperties() {
  61. Properties properties = new Properties();
  62. properties.setProperty("hibernate.hbm2ddl.auto", "update");
  63. properties.setProperty("hibernate.show_sql", "true");
  64. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  65. return properties;
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement