Advertisement
Guest User

Untitled

a guest
May 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. package com.amanda.curso.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  6. import org.springframework.orm.jpa.JpaTransactionManager;
  7. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  8. import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
  9. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  10.  
  11. import javax.persistence.EntityManagerFactory;
  12. import javax.sql.DataSource;
  13. import java.util.Properties;
  14.  
  15. @Configuration
  16. public class SpringJpaConfig {
  17.  
  18. @Bean
  19. public DataSource dataSource() {
  20. DriverManagerDataSource ds = new DriverManagerDataSource();
  21. ds.setDriverClassName("com.mysql.jdbc.Driver");
  22. ds.setUrl("jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true");
  23. ds.setUsername("root");
  24. ds.setPassword("root");
  25. return ds;
  26. }
  27.  
  28. @Bean
  29. public EntityManagerFactory entityManagerFactory(){
  30. LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  31. factory.setDataSource(dataSource());
  32. factory.setPackagesToScan("com.amanda.curso.domain");
  33. factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  34. factory.setJpaProperties(jpaProperties());
  35. factory.afterPropertiesSet();
  36. return factory.getObject();
  37. }
  38.  
  39. @Bean
  40. public JpaTransactionManager transactionManager() {
  41. JpaTransactionManager tx = new JpaTransactionManager();
  42. tx.setEntityManagerFactory(entityManagerFactory());
  43. tx.setJpaDialect(new HibernateJpaDialect());
  44. return tx;
  45. }
  46.  
  47. private Properties jpaProperties() {
  48. Properties props = new Properties();
  49. props.setProperty("hibernate.show_sql", "true");
  50. props.setProperty("hibernate.format_sql", "true");
  51. props.setProperty("hibernate.hbm2ddl.auto", "update");
  52. return props;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement