Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableJpaRepositories(basePackages = "com.core.jpa.repository")
- @EntityScan(basePackages = "com.core.jpa.entity")
- @EnableConfigurationProperties(value = {EclipseLinkProperties.class, EntityManagerProperties.class})
- public class WebDatasourceConfig {
- @Autowired
- private EclipseLinkProperties eclipseLinkProperties;
- @Autowired
- private EntityManagerProperties entityManagerProperties;
- /**
- * Get data source.
- *
- * @return Data source
- */
- @Primary
- @Bean
- @ConfigurationProperties(prefix="spring.datasource")
- public DataSource getDatasource() {
- return DataSourceBuilder.create().build();
- }
- /**
- * EntityManager configuration.
- */
- @Bean(name = "entityManagerFactory")
- public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
- final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
- em.setDataSource(getDatasource());
- em.setJpaDialect(jpaDialect());
- em.setPackagesToScan(entityManagerProperties.getPackagesToScan());
- // em.setPersistenceUnitName(eclipseLinkProperties.getPersistenceUnitName());
- final DatabasePlatform dp = new MySQLPlatform();
- em.setJpaVendorAdapter(getEclipseLinkJpaVendorAdapter());
- //following code will be used for static weaving. Uncomment when creating war.
- final Map<String, String> propMap = new HashMap<String, String>();
- propMap.put("eclipselink.weaving", eclipseLinkProperties.getWeaving());
- // propMap.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
- // propMap.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_BOTH_GENERATION);
- // propMap.put(PersistenceUnitProperties.CREATE_JDBC_DDL_FILE, "create.sql");
- em.setJpaPropertyMap(propMap);
- // em.setLoadTimeWeaver(loadTimeWeaver()); //comment this when using static weaving. Mostly in development environment inside eclipse
- return em;
- }
- /**
- * Exposes EclipseLink's persistence provider and EntityManager extension interface,
- * and adapts AbstractJpaVendorAdapter's common configuration settings.
- */
- @Bean
- public EclipseLinkJpaVendorAdapter getEclipseLinkJpaVendorAdapter() {
- final EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
- vendorAdapter.setDatabasePlatform(eclipseLinkProperties.getDatabasePlatform());
- vendorAdapter.setGenerateDdl(eclipseLinkProperties.isGenerateDll());
- vendorAdapter.setShowSql(eclipseLinkProperties.isShowSql());
- return vendorAdapter;
- }
- /**
- * Create a new InstrumentationLoadTimeWeaver for the default ClassLoader.
- */
- @Bean()
- public LoadTimeWeaver loadTimeWeaver() {
- return new org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver();
- }
- /**
- * This is the central interface in Spring's transaction support.
- * Applications can use this directly, but it is not primarily meant as API.
- */
- @Bean
- public PlatformTransactionManager transactionManager() {
- final JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
- return transactionManager;
- }
- /**
- * Bean post-processor that automatically applies persistence exception translation
- * to any bean marked with Spring's @Repository annotation, adding a corresponding
- * PersistenceExceptionTranslationAdvisor to the exposed proxy
- * (either an existing AOP proxy or a newly generated proxy that implements
- * all of the target's interfaces).
- */
- @Bean
- public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
- return new PersistenceExceptionTranslationPostProcessor();
- }
- /**
- * SPI strategy that encapsulates certain functionality that standard JPA 2.1 does not offer,
- * such as access to the underlying JDBC Connection.
- * This strategy is mainly intended for standalone usage of a JPA provider.
- */
- @Bean
- public JpaDialect jpaDialect() {
- return new EclipseLinkJpaDialect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement