Java tutorial
/* * iBankApp * * License : Apache License,Version 2.0, January 2004 * * See the LICENSE file in English or LICENSE.zh_CN in chinese * in the root directory or <http://www.apache.org/licenses/>. */ package org.ibankapp.base.persistence.validation.test; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.ibankapp.base.persistence.repository.JpaRepository; import org.ibankapp.base.persistence.repository.JpaRepositoryImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; @Configuration @EnableTransactionManagement public class TestContextConfig { @Bean DataSource dataSource() { HikariConfig dataSourceConfig = new HikariConfig(); dataSourceConfig.setDriverClassName("org.h2.Driver"); dataSourceConfig.setJdbcUrl("jdbc:h2:mem:ibankapp"); return new HikariDataSource(dataSourceConfig); } @Bean JpaRepository repository() { return new JpaRepositoryImpl(); } @Bean LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource); entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); entityManagerFactoryBean.setPackagesToScan("org.ibankapp"); Properties properties = new Properties(); properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); properties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy"); properties.put("hibernate.hbm2ddl.auto", "update"); properties.put("hibernate.show_sql", true); entityManagerFactoryBean.setJpaProperties(properties); return entityManagerFactoryBean; } @Bean JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory); return transactionManager; } }