title: static weaving eclipselink jpa with spring and tomcat
body: <p>I am trying to achieve static weaving with Eclipselink+Spring+JPA using maven. I have configured my spring project with Java Configuration, no context.xml. I tried following things:</p>
  
  <ol>
  <li>In persistence.xml:</li>
  </ol>
  
  <p>Added following weaving specification.</p>
  
  <pre><code>&lt;property name="eclipselink.weaving" value="static" /&gt;
  </code></pre>
  
  <ol start="2">
  <li>In DBConfig.java, I have configured beans </li>
  </ol>
  
  <p>as follow :</p>
  
  <pre><code> @Bean()
      public DataSource getDataSource() {
          BoneCPDataSource ds = new BoneCPDataSource();
          log.debug("Driver Name : " + driverClassName);
          try {
              Class.forName(driverClassName);
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          log.debug("DB Url : " + url);
          ds.setDriverClass(driverClassName);
          ds.setJdbcUrl(url);
          ds.setUsername(username);
          ds.setPassword(password);
          ds.setMaxConnectionsPerPartition(5);
          ds.setMinConnectionsPerPartition(2);
          ds.setAcquireIncrement(2);
          ds.setDefaultAutoCommit(false);     
          Properties props = new Properties();
          props.put("eclipselink.weaving", "static");
          try {
              ds.setProperties(props);
          } catch (Exception e) {         
              e.printStackTrace();
          }
          return ds;
      }
  
      @Bean(name = "em")
      public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(getDataSource());      
          em.setJpaDialect(jpaDialect());
          em.setPackagesToScan("com.cdl.hp50.model", "com.cdl.hp50.model.person");
          em.setPersistenceUnitName(persistenceUnitName);
          DatabasePlatform dp = new MySQLPlatform();
          em.setJpaVendorAdapter(getEclipseLinkJpaVendorAdapter());       
          return em;
      }
  </code></pre>
  
  <ol start="3">
  <li>In pom.xml, I have added following plugin as per stated on <a href="https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving" rel="nofollow">eclipselink documentation</a>:</li>
  </ol>
  
  <p>As follow: </p>
  
  <pre><code>              &lt;plugin&gt;
                      &lt;groupId&gt;de.empulse.eclipselink&lt;/groupId&gt;
                      &lt;artifactId&gt;staticweave-maven-plugin&lt;/artifactId&gt;
                      &lt;version&gt;1.0.0&lt;/version&gt;
                      &lt;executions&gt;
                          &lt;execution&gt;
                              &lt;phase&gt;process-classes&lt;/phase&gt;
                              &lt;goals&gt;
                                  &lt;goal&gt;weave&lt;/goal&gt;
                              &lt;/goals&gt;
                              &lt;configuration&gt;
                                  &lt;persistenceXMLLocation&gt;META-INF/persistence.xml&lt;/persistenceXMLLocation&gt;
                                  &lt;logLevel&gt;FINE&lt;/logLevel&gt;
                              &lt;/configuration&gt;
                          &lt;/execution&gt;
                      &lt;/executions&gt;
                      &lt;dependencies&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
                              &lt;artifactId&gt;org.eclipse.persistence.jpa&lt;/artifactId&gt;
                              &lt;version&gt;${eclipselink.version}&lt;/version&gt;
                          &lt;/dependency&gt;
                      &lt;/dependencies&gt;
                  &lt;/plugin&gt;
  </code></pre>
  
  <p>I have specified clearly to use static (build time) weaving. And while I create war using <code>maven install</code>, entities listed in persistence.xml are weaved successfully. But when I deploy war into the tomcat, it gives following exception:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'em' defined in class com.cdl.hp50.config.DBConfig: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4992)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
      at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1245)
      at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1895)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
      at java.util.concurrent.FutureTask.run(FutureTask.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
      at org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:80)
      at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:348)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:341)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
      ... 25 more
  </code></pre>
  
  <p>I have event tried setting <code>eclipselink.weaving</code> as <code>false</code>. But still it gives same error. I don't know why it is looking for <code>LoadTimeWeaver</code>. Please suggest me what I am missing over here.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I have uploaded starter project that includes eclipselink, spring mvc, spring data jpa, sitemesh on <a href="https://github.com/gharia/spring-eclipselink-javaconfig" rel="nofollow">github</a></p>