静态编织eclipselink jpa与spring和tomcat [英] static weaving eclipselink jpa with spring and tomcat

查看:61
本文介绍了静态编织eclipselink jpa与spring和tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Maven使用Eclipselink + Spring + JPA实现静态编织。我已经用Java Configuration(没有context.xml)配置了spring项目。我尝试了以下操作:

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:


  1. 在persistence.xml中:

根据编织规范添加。

<property name="eclipselink.weaving" value="static" />




  1. 在DBConfig.java中,我已经配置了bean

如下:

 @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;
    }




  1. 在pom.xml中,我已按照 eclipselink文档

  1. In pom.xml, I have added following plugin as per stated on eclipselink documentation:

如下:

              <plugin>
                    <groupId>de.empulse.eclipselink</groupId>
                    <artifactId>staticweave-maven-plugin</artifactId>
                    <version>1.0.0</version>
                    <executions>
                        <execution>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>weave</goal>
                            </goals>
                            <configuration>
                                <persistenceXMLLocation>META-INF/persistence.xml</persistenceXMLLocation>
                                <logLevel>FINE</logLevel>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.eclipse.persistence</groupId>
                            <artifactId>org.eclipse.persistence.jpa</artifactId>
                            <version>${eclipselink.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>

我明确指定使用静态(构建时间)编织。当我使用 maven install 发动战争时,persistence.xml中列出的实体被成功编织。但是,当我向雄猫部署战争时,它会给出以下异常:

I have specified clearly to use static (build time) weaving. And while I create war using maven install, entities listed in persistence.xml are weaved successfully. But when I deploy war into the tomcat, it gives following exception:

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

我尝试将 eclipselink.weaving 设置为 false 。但是仍然会给出相同的错误。我不知道为什么要寻找 LoadTimeWeaver 。请向我建议我在这里缺少的内容。

I have event tried setting eclipselink.weaving as false. But still it gives same error. I don't know why it is looking for LoadTimeWeaver. Please suggest me what I am missing over here.

更新:

我已在 github 上上传了包含eclipselink,spring mvc,spring data jpa,sitemesh的启动程序项目。 / p>

I have uploaded starter project that includes eclipselink, spring mvc, spring data jpa, sitemesh on github

推荐答案

编辑:我认为我发现了您的问题

   props.put("eclipselink.weaving", "static");
    try {
        ds.setProperties(props);

您是否要对数据源应用eclipselink属性?如果您查看我的示例context.xml(形成下面的应用程序),则需要将jpaPropertyMap应用于LocalContainerEntityManagerFactoryBean。

Are you trying to apply eclipselink properties to your datasource? If you check my example context.xml (form the app below), the jpaPropertyMap needs to be applied to the LocalContainerEntityManagerFactoryBean.

因此,应将代码更改为

 em.setjpaPropertyMap(props)

旧答案:

首先,对于此类问题,一个示例项目会非常好。好吧,我为您做到了,可以在以下位置找到它: https://github.com/baumgartner / eclipselink-static-weaving

First of all, for such questions, an example project would be really good. Well, i did it for you, it can be found at: https://github.com/baumgartner/eclipselink-static-weaving

还有一个坏消息:我无法重现您的问题,我的应用程序在tomcat 7上运行良好。

And the bad news: I can't reproduce your problem, my Application works fine on tomcat 7.

是否检查了.class文件,如果它们被static-weaving-plugin扩展了?编织的文件是否在您的tomcat文件夹中? tomcat wtpwebapps-folder中的文件正确吗? (有时同步只是失败)
您是否使用JAD(java反编译器)对类进行反编译并检查它们是否得到了增强?增强的实体如下所示:

Did you check your .class files if they got extended by the static-weaving-plugin? Are the weaved files in your tomcat folder? Are the correct files in tomcat wtpwebapps-folder? (Sometimes synchronisation just fails) Did you use JAD (java decompiler) to decompile your classes and check if they got enhanced? An enhanced entity would look like this:

public class asdf.User implements java.lang.Cloneable, org.eclipse.persistence.internal.weaving.PersistenceWeaved, org.eclipse.persistence.internal.descriptors.PersistenceEntity, org.eclipse.persistence.internal.descriptors.PersistenceObject, org.eclipse.persistence.queries.FetchGroupTracker, org.eclipse.persistence.internal.weaving.PersistenceWeavedFetchGroups, org.eclipse.persistence.descriptors.changetracking.ChangeTracker, org.eclipse.persistence.internal.weaving.PersistenceWeavedChangeTracking, org.eclipse.persistence.internal.weaving.PersistenceWeavedRest {

如果部署战争,您的应用程序可以工作吗?如果您从IDE运行应用程序,是否可以正常工作?

Does your app work if you deploy the war? Does it work if you run your app from the IDE?

这篇关于静态编织eclipselink jpa与spring和tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆