Spring使用内存数据库二
在上篇《Spring 使用内存数据库》中我们使用Hibernate作为ORM的框架,直接调用Hibernate的API进行相关的持久化操作,但在实际项目中,可能会由于公司规定等原因要求统一使用JPA接口来进行持久化,而JPA的实现可以灵活配置,避免对第三方JPA实现的过分依赖,这样可能会牺牲第三方JPA实现带来的一些额外优化和特性,但确实避免了对其依赖,本文将更改《Spring 使用内存数据库》Demo的实现,使其使用JPA标准接口进行持久化操作,而JPA实现可以配置为Hibernate或EclipseLink支持。
第一步:将HibernateConfiguration类修改为JpaConfiguration类
1 @Configuration 2 public class JpaConfiguration { 3 @Autowired 4 private DataSource dataSource; 5 @Bean 6 public Map<String, Object> jpaProperties() { 7 Map<String, Object> props = new HashMap<String, Object>(); 8 //Hibernate JPA properties 9 10 /*props.put("hibernate.dialect", H2Dialect.class.getName()); 11 props.put("hibernate.cache.provider_class", HashtableCacheProvider.class.getName());*/ 12 13 //EclipseLink JPA properties 14 15 props.put("eclipselink.weaving", "false"); 16 17 return props; 18 } 19 @Bean 20 public JpaVendorAdapter jpaVendorAdapter() { 21 //Hibernate JPA Vendor Adapter 22 /*AbstractJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();*/ 23 24 //EclipseLink JPA Vendor Adapter 25 AbstractJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter(); 26 27 //Common vendor settings 28 jpaVendorAdapter.setShowSql(false); 29 jpaVendorAdapter.setGenerateDdl(true); 30 jpaVendorAdapter.setDatabase(Database.H2);//指定数据库为 H2 31 32 return jpaVendorAdapter; 33 } 34 @Bean 35 public PlatformTransactionManager transactionManager() { 36 return new JpaTransactionManager( localContainerEntityManagerFactoryBean().getObject() ); 37 } 38 @Bean 39 public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() { 40 LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); 41 lef.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver()); 42 lef.setDataSource(this.dataSource); 43 lef.setJpaPropertyMap(this.jpaProperties()); 44 lef.setJpaVendorAdapter(this.jpaVendorAdapter()); 45 return lef; 46 } 47 48 /** 49 * 设置数据源,可以指定类型为Derby或HSQLDB 50 * @return 51 */ 52 @Bean 53 public DataSource dataSource(){ 54 EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 55 builder.setType(EmbeddedDatabaseType.H2); 56 return builder.build(); 57 } 58 }
第二步:修改OrderPersistenceTests类
将org.hibernate.Session类的API操作修改为javax.persistence.EntityManager类的API
org.hibernate.Session类的API代码示例:
1 @Autowired 2 private SessionFactory sessionFactory; 3 @Test 4 @Transactional 5 public void testSaveOrderWithItems() throws Exception { 6 Session session = sessionFactory.getCurrentSession(); 7 Order order = new Order(); 8 order.getItems().add(new Item()); 9 session.save(order); 10 session.flush(); 11 assertNotNull(order.getId()); 12 }
javax.persistence.EntityManager类的API代码示例:
1 @PersistenceContext 2 private EntityManager entityManager; 3 @Test 4 @Transactional 5 public void testSaveOrderWithItems() throws Exception { 6 Order order = new Order(); 7 order.getItems().add(new Item()); 8 entityManager.persist(order); 9 entityManager.flush(); 10 assertNotNull(order.getId()); 11 }
第三步:修改Maven依赖
修改完成,可以开始运行测试用例进行调试!总感觉EclipseLink是个半成品,不是很好使,甲骨文公司还是希望Everybody使用TopLink!