Advertisement
Guest User

conf

a guest
Apr 15th, 2018
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. @Configuration
  2. @EnableJpaRepositories(basePackages = "com.core.jpa.repository")
  3. @EntityScan(basePackages = "com.core.jpa.entity")
  4. @EnableConfigurationProperties(value = {EclipseLinkProperties.class, EntityManagerProperties.class})
  5. public class WebDatasourceConfig {
  6.  
  7.     @Autowired
  8.     private EclipseLinkProperties eclipseLinkProperties;
  9.     @Autowired
  10.     private EntityManagerProperties entityManagerProperties;
  11.  
  12.     /**
  13.      * Get data source.
  14.      *
  15.      * @return Data source
  16.      */
  17.     @Primary
  18.     @Bean
  19.     @ConfigurationProperties(prefix="spring.datasource")
  20.     public DataSource getDatasource() {
  21.         return DataSourceBuilder.create().build();
  22.     }
  23.  
  24.     /**
  25.      * EntityManager configuration.
  26.      */
  27.     @Bean(name = "entityManagerFactory")
  28.     public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
  29.         final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  30.         em.setDataSource(getDatasource());
  31.         em.setJpaDialect(jpaDialect());
  32.         em.setPackagesToScan(entityManagerProperties.getPackagesToScan());
  33. //        em.setPersistenceUnitName(eclipseLinkProperties.getPersistenceUnitName());
  34.         final DatabasePlatform dp = new MySQLPlatform();
  35.         em.setJpaVendorAdapter(getEclipseLinkJpaVendorAdapter());
  36.  
  37.         //following code will be used for static weaving. Uncomment when creating war.
  38.         final Map<String, String> propMap = new HashMap<String, String>();
  39.         propMap.put("eclipselink.weaving", eclipseLinkProperties.getWeaving());
  40. //        propMap.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
  41. //        propMap.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_BOTH_GENERATION);
  42. //        propMap.put(PersistenceUnitProperties.CREATE_JDBC_DDL_FILE, "create.sql");
  43.         em.setJpaPropertyMap(propMap);
  44.  
  45. //        em.setLoadTimeWeaver(loadTimeWeaver()); //comment this when using static weaving. Mostly in development environment inside eclipse
  46.         return em;
  47.     }
  48.  
  49.     /**
  50.      * Exposes EclipseLink's persistence provider and EntityManager extension interface,
  51.      * and adapts AbstractJpaVendorAdapter's common configuration settings.
  52.      */
  53.     @Bean
  54.     public EclipseLinkJpaVendorAdapter getEclipseLinkJpaVendorAdapter() {
  55.         final EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
  56.         vendorAdapter.setDatabasePlatform(eclipseLinkProperties.getDatabasePlatform());
  57.         vendorAdapter.setGenerateDdl(eclipseLinkProperties.isGenerateDll());
  58.         vendorAdapter.setShowSql(eclipseLinkProperties.isShowSql());
  59.         return vendorAdapter;
  60.     }
  61.  
  62.     /**
  63.      * Create a new InstrumentationLoadTimeWeaver for the default ClassLoader.
  64.      */
  65.     @Bean()
  66.     public LoadTimeWeaver loadTimeWeaver() {
  67.         return new org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver();
  68.     }
  69.  
  70.     /**
  71.      * This is the central interface in Spring's transaction support.
  72.      * Applications can use this directly, but it is not primarily meant as API.
  73.      */
  74.     @Bean
  75.     public PlatformTransactionManager transactionManager() {
  76.         final JpaTransactionManager transactionManager = new JpaTransactionManager();
  77.         transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
  78.         return transactionManager;
  79.     }
  80.  
  81.     /**
  82.      * Bean post-processor that automatically applies persistence exception translation
  83.      * to any bean marked with Spring's @Repository annotation, adding a corresponding
  84.      * PersistenceExceptionTranslationAdvisor to the exposed proxy
  85.      * (either an existing AOP proxy or a newly generated proxy that implements
  86.      * all of the target's interfaces).
  87.      */
  88.     @Bean
  89.     public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
  90.         return new PersistenceExceptionTranslationPostProcessor();
  91.     }
  92.  
  93.     /**
  94.      * SPI strategy that encapsulates certain functionality that standard JPA 2.1 does not offer,
  95.      * such as access to the underlying JDBC Connection.
  96.      * This strategy is mainly intended for standalone usage of a JPA provider.
  97.      */
  98.     @Bean
  99.     public JpaDialect jpaDialect() {
  100.         return new EclipseLinkJpaDialect();
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement