Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ...;
- import java.util.Properties;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.JpaVendorAdapter;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- @Configuration
- @EnableTransactionManagement
- public class JPAConfiguration {
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
- LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
- em.setDataSource(dataSource);
- em.setPackagesToScan(new String[] { "caminho.meu" });
- JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
- em.setJpaVendorAdapter(vendorAdapter);
- em.setJpaProperties(additionalProperties());
- return em;
- }
- @Bean(name = "dataSource")
- public DriverManagerDataSource dataSource() {
- DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
- driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
- driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/meudb");
- driverManagerDataSource.setUsername("ruti");
- driverManagerDataSource.setPassword("ruti");
- return driverManagerDataSource;
- }
- @Bean
- public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(emf);
- return transactionManager;
- }
- @Bean
- public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
- return new PersistenceExceptionTranslationPostProcessor();
- }
- Properties additionalProperties() {
- Properties properties = new Properties();
- properties.setProperty("hibernate.hbm2ddl.auto", "update");
- properties.setProperty("hibernate.show_sql", "true");
- properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
- return properties;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement