Advertisement
passella

Untitled

Jan 26th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. package br.com.passella.config;
  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.ComponentScan;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
  12. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  13. import org.springframework.orm.jpa.JpaTransactionManager;
  14. import org.springframework.orm.jpa.JpaVendorAdapter;
  15. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  16. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  17. import org.springframework.transaction.PlatformTransactionManager;
  18. import org.springframework.transaction.annotation.EnableTransactionManagement;
  19.  
  20. @Configuration
  21. @ComponentScan(basePackages = { "br.com.passella.model" })
  22. @EnableTransactionManagement
  23. public class PersistenceJPAConfig {
  24.  
  25.     @Bean
  26.     public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  27.         LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  28.         em.setDataSource(dataSource());
  29.         em.setPackagesToScan(new String[] { "br.com.passella.model" });
  30.  
  31.         JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  32.         em.setJpaVendorAdapter(vendorAdapter);
  33.         em.setJpaProperties(additionalProperties());
  34.  
  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/sakila?useSSL=false");
  43.         dataSource.setUsername("root");
  44.         dataSource.setPassword("senhasql");
  45.         return dataSource;
  46.     }
  47.  
  48.     @Bean
  49.     public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
  50.         JpaTransactionManager transactionManager = new JpaTransactionManager();
  51.         transactionManager.setEntityManagerFactory(emf);
  52.  
  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.         return properties;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement