mellowdeep

configfile

Nov 20th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package bg.softuni.hibernate.configuration;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.PropertySource;
  7. import org.springframework.core.env.Environment;
  8. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  9. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  10. import org.springframework.orm.jpa.JpaTransactionManager;
  11. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  12. import org.springframework.orm.jpa.vendor.Database;
  13. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  14. import org.springframework.transaction.PlatformTransactionManager;
  15. import org.springframework.transaction.annotation.EnableTransactionManagement;
  16.  
  17. import javax.persistence.EntityManagerFactory;
  18. import javax.sql.DataSource;
  19. import java.util.Properties;
  20.  
  21. @Configuration
  22. @EnableJpaRepositories(basePackages = "bg.softuni.hibernate.repositories")
  23. @EnableTransactionManagement
  24. @PropertySource(value = "application.properties")
  25. public class JavaConfig {
  26.  
  27.     private final Environment environment;
  28.  
  29.     @Autowired
  30.     public JavaConfig(Environment environment) {
  31.         this.environment = environment;
  32.     }
  33.  
  34.     @Bean
  35.     public DataSource dataSource() {
  36.         DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
  37.         driverManagerDataSource.setDriverClassName(this.environment
  38.                 .getProperty("spring.datasource.driverClassName"));
  39.         driverManagerDataSource.setUrl(this.environment
  40.                 .getProperty("spring.datasource.url"));
  41.         driverManagerDataSource.setUsername(this.environment
  42.                 .getProperty("spring.datasource.username"));
  43.         driverManagerDataSource.setPassword(this.environment
  44.                 .getProperty("spring.datasource.password"));
  45.         return driverManagerDataSource;
  46.     }
  47.  
  48.     @Bean
  49.     public EntityManagerFactory entityManagerFactory() {
  50.         HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  51.         vendorAdapter.setDatabase(Database.MYSQL);
  52.         vendorAdapter.setGenerateDdl(true);
  53.         vendorAdapter.setShowSql(true);
  54.         LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  55.         factory.setJpaVendorAdapter(vendorAdapter);
  56.         factory.setPackagesToScan("bg.softuni.hibernate.entities");
  57.         factory.setDataSource(this.dataSource());
  58.         Properties jpaProperties = new Properties();
  59.         jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create");
  60.         jpaProperties.setProperty("hibernate.format_sql", "true");
  61.         factory.setJpaProperties(jpaProperties);
  62.         factory.afterPropertiesSet();
  63.         return factory.getObject();
  64.     }
  65.  
  66.     @Bean
  67.     public PlatformTransactionManager transactionManager() {
  68.         JpaTransactionManager txManager = new JpaTransactionManager();
  69.         txManager.setEntityManagerFactory(this.entityManagerFactory());
  70.         return txManager;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment