Advertisement
Guest User

Untitled

a guest
Sep 4th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package org.lightadmin.demo.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.data.jpa.repository.config.EnableJpaRepositories;
  7. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  8. import org.springframework.orm.jpa.JpaTransactionManager;
  9. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  10. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  11. import org.springframework.transaction.annotation.EnableTransactionManagement;
  12.  
  13. import javax.sql.DataSource;
  14. import java.util.Properties;
  15.  
  16. import static org.springframework.orm.jpa.vendor.Database.MYSQL;
  17.  
  18. @Configuration
  19. @EnableTransactionManagement
  20. @EnableJpaRepositories(basePackages = {"org.lightadmin.demo.persistence"})
  21. public class ApplicationConfiguration {
  22.  
  23. @Bean
  24. @Autowired
  25. public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
  26. final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  27. vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
  28. vendorAdapter.setDatabase(MYSQL);
  29. vendorAdapter.setGenerateDdl(true);
  30. vendorAdapter.setShowSql(false);
  31.  
  32. final LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  33. factory.setJpaVendorAdapter(vendorAdapter);
  34. factory.setPackagesToScan("org.lightadmin.demo.domain");
  35. factory.setDataSource(dataSource);
  36.  
  37. factory.setJpaProperties(jpaProperties());
  38.  
  39. return factory;
  40. }
  41.  
  42. @Bean
  43. public JpaTransactionManager transactionManager() {
  44. return new JpaTransactionManager();
  45. }
  46.  
  47. @Bean
  48. public DataSource dataSource() {
  49. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  50. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  51. dataSource.setUrl("jdbc:mysql://localhost:3306/lightadmin-demo?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf8");
  52. dataSource.setUsername("root");
  53. dataSource.setPassword("sun1ssh1n1ng");
  54. return dataSource;
  55. }
  56.  
  57. private Properties jpaProperties() {
  58. final Properties jpaProperties = new Properties();
  59. jpaProperties.setProperty("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
  60. return jpaProperties;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement