RAD中配置 Spring JPA



J2SE:

1. use LocalContainerEntityManagerFactoryBean

 

public LocalContainerEntityManagerFactoryBean entityManagerFactory(
   DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
  LocalContainerEntityManagerFactoryBean emfb = 
    new LocalContainerEntityManagerFactoryBean();
  emfb.setDataSource(dataSource);
  emfb.setJpaVendorAdapter(jpaVendorAdapter);
  emfb.setPackagesToScan("getorder");    // 包含 JPA entity 的 Java 包.
  emfb.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());   
  return emfb;
 }

2. 用 @Bean 注入 Derby data source.

public LocalContainerEntityManagerFactoryBean entityManagerFactory(
   DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
  LocalContainerEntityManagerFactoryBean emfb = 
    new LocalContainerEntityManagerFactoryBean();
  emfb.setDataSource(dataSource);
  emfb.setJpaVendorAdapter(jpaVendorAdapter);
  emfb.setPackagesToScan("getorder");    // 包含 JPA entity 的 Java 包.
  emfb.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());   
  return emfb;
 }

3. 用 @Bean 注入 JpaVendorAdapter. 开始使用 OpenJpa,后来改成 EclipseLink 实现。

public LocalContainerEntityManagerFactoryBean entityManagerFactory(
   DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
  LocalContainerEntityManagerFactoryBean emfb = 
    new LocalContainerEntityManagerFactoryBean();
  emfb.setDataSource(dataSource);
  emfb.setJpaVendorAdapter(jpaVendorAdapter);
  emfb.setPackagesToScan("getorder");    // 包含 JPA entity 的 Java 包.
  emfb.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());   
  return emfb;
 }

4.  Exception :

"main" java.lang.NoSuchMethodError: org/springframework/util/ReflectionUtils.doWithLocalFields(Ljava/lang/Class;Lorg/springframework/util/ReflectionUtils$FieldCallback;)V
 at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:418)
 at org.springfr....
 
原因是混淆了 spring 不同模块的版本,统一替换成 4.3.6版本。
spring-beans, spring-core, spring-context, spring-context-support, spring-aop; spring-data-commons, spring-data-jpa, spring-jdbc, spring-orm, spring-tx.


5. ClassNotFound:  MapBackedSet

下载: apache: commons-collections-3.2.jar


6. 安装 EclipseLink JPA 实现:

下载eclipselink-2.6.4.v20160829-44060b6.zip;解开;把jlib/eclipselink.jar, jlib/jpa/*.jar 拷贝到 /lib 路径下.


7. Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/marmara.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:341)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1073)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

解决:
1) 创建 LocalContainerEntityManagerFactoryBean 时指定 LoadTimeWeaver:
  emfb.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());  
2) 在 JVM 参数中加入 -javaagent:


测试:
1. TestMain:

 public static void main(String[] args) {
  ApplicationContext  ac = new AnnotationConfigApplicationContext(OrderConfiguration.class);
  
  ProfileRegistry reg = ac.getBean(ProfileRegistry.class);
  reg.testJPA();
 }

2. ProfileRegistry.testJPA:

 @Autowired
 private EntityManagerFactory entityManagerFactory; 
 public EntityManager getEntityManager() {
  return entityManagerFactory.createEntityManager();
 }
 public void testJPA(){
  System.out.println("JPA entityManager impl: " + getEntityManager().getClass().getName());
  FetcherConfig fc = getEntityManager().find(FetcherConfig.class, new Integer(1));
  System.out.print("Found fetcher config: " + fc.getName());
 }

测试结果:

Feb 24, 2017 10:36:19 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1279adbb: startup date [Fri Feb 24 10:36:19 CST 2017]; root of context hierarchy
Feb 24, 2017 10:36:20 AM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Feb 24, 2017 10:36:20 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.apache.derby.jdbc.EmbeddedDriver
Feb 24, 2017 10:36:20 AM org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
INFO: Building JPA container EntityManagerFactory for persistence unit 'default'
Feb 24, 2017 10:36:20 AM org.springframework.orm.jpa.AbstractEntityManagerFactoryBean buildNativeEntityManagerFactory
INFO: Initialized JPA EntityManagerFactory for persistence unit 'default'
Feb 24, 2017 10:36:20 AM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
[EL Info]: 2017-02-24 10:36:20.957--ServerSession(2008245965)--EclipseLink, version: Eclipse Persistence Services - 2.6.4.v20160829-44060b6
[EL Info]: connection: 2017-02-24 10:36:21.427--ServerSession(2008245965)--/file:/W:bin/_default login successful
JPA entityManager impl: com.sun.proxy.$Proxy14
[EL Fine]: sql: 2017-02-24 10:36:21.468--ServerSession(2008245965)--Connection(427635119)--SELECT FETCHER_ID, DATASET, IMPLCLASS, NAME, RESOURCE FROM FETCHERCONFIG WHERE (FETCHER_ID = ?)
 bind => [1 parameter bound]
Found fetcher config:  OrderBasic


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值