TOMEE jpa-eclipselink示例不起作用

时间:2015-12-30 14:11:23

标签: java-ee ejb eclipselink tomee openejb

TOMEE jpa-eclipselink示例:

https://github.com/apache/tomee/tree/trunk/examples/jpa-eclipselink

不为我工作

单元测试类(MoviesTest)中的

EJBContainer.createEJBContainer(p)失败,但出现以下异常:

...
INFO - Found EjbModule in classpath: c:\users\oren\projects\test\jpa-eclipselink\target\classes
INFO - Beginning load: c:\users\oren\projects\test\jpa-eclipselink\target\classes
INFO - Configuring enterprise application: C:\Users\Oren\Projects\Test\jpa-eclipselink
INFO - Closing DataSource: movieDatabase

java.lang.NoSuchMethodError: javax.ejb.Stateful.passivationCapable()Z
    at org.apache.openejb.config.AnnotationDeployer$DiscoverAnnotatedBeans.deploy(AnnotationDeployer.java:1454)
    at org.apache.openejb.config.AnnotationDeployer$DiscoverAnnotatedBeans.deploy(AnnotationDeployer.java:456)
    at org.apache.openejb.config.AnnotationDeployer.deploy(AnnotationDeployer.java:371)
    at org.apache.openejb.config.ConfigurationFactory$Chain.deploy(ConfigurationFactory.java:415)
    at org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:1002)
    at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:321)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
    at org.superbiz.eclipselink.MoviesTest.test(MoviesTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我试图google java.lang.NoSuchMethodError:javax.ejb.Stateful.passivationCapable ,但没有找到任何线索

我对示例maven项目所做的唯一更改是:

<dependency>
  <groupId>org.apache.openejb</groupId>
  <artifactId>javaee-api</artifactId>
  <version>6.0-6</version>
  <scope>provided</scope>
</dependency>

而不是:

<dependency> 
 <groupId>org.apache.openejb</groupId> 
 <artifactId>javaee-api</artifactId> 
 <version>7.0-SNAPSHOT</version> 
 <scope>provided</scope> 
</dependency> 

因为第一个不会构建。

任何人都可以解释这是什么问题,我已经花了几个小时没有成功。

最好的问候

3 个答案:

答案 0 :(得分:1)

如果您查看javax.ejb.Stateful.passivationCapable的JEE7-javadoc,您会发现

  

自:EJB 3.2

因此,只有JEE7引入了此方法,因为JEE6使用了EJB 3.1。

你使用的eclipselink-version似乎需要一个JEE7实现(这就是为什么他们把它放在他们的依赖列表中)并且不能用于JEE6。

答案 1 :(得分:0)

如果您无法使用JavaEE 6.0.6进行构建, 我通过eclipselink,TomEE 1.7.2,Mysql为您提供了我可以构建和运行的设置。

事实上,我正在尝试做与你完全相同的事情(对我来说,花了几天时间),几分钟前,我终于设法用eclipselink运行我的应用程序!耶!

Bellow是我的pom.xml的样子:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <!-- some of my project settings here -->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <finalName>myprojectname</finalName>
        <plugins>
            <plugin>
                <!-- org.apache.maven.plugins 3.3 for source v1.8 -->
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots/>
            <id>apache-maven-snapshots</id>
            <url>https://repository.apache.org/content/groups/snapshots</url>
        </repository>
        <repository>
            <id>eclipselink-repo</id>
            <name>EclipseLink Repository</name>
            <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
        </repository>
    </repositories>

    <dependencies>

        <!-- mojarra required for facesmessage on ViewExpiredException -->
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
                <version>2.2.12</version>
        </dependency>
        <!-- /mojarra -->

        <!-- db connection required -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <!-- /db connection -->

        <!-- JavaEE required -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- /JavaEE -->

        <!-- OmniFaces required for JSF, @Eager, postback same request parameters, etc -->
        <dependency>
            <groupId>org.omnifaces</groupId>
            <artifactId>omnifaces</artifactId>
            <version>1.8.3</version>
        </dependency>
        <!-- /OmniFaces -->

        <!-- some commons-io, commons-lang3, jackson, velocity, aws sdks not relevant i guess. -->

        <!-- even thought this is in the tomee eclipselink sample, it is not required -->
        <!--
        <dependency>
            <groupId>org.apache.openejb</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0-6</version>
        </dependency>
        -->

        <!-- openejb container -->
        <dependency>
            <groupId>org.apache.openejb</groupId>
            <artifactId>openejb-core</artifactId>
            <version>4.7.2</version>
            <scope>provided</scope>
        </dependency>

        <!-- toplink dependencies -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.4.2</version>
        </dependency>

    </dependencies>

    <!-- <distributionManagement> section , i guess not relevant either. -->

</project>

我的persistence.xml看起来像:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="1.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="myprojectname-persistence-unit" transaction-type="JTA">
        <jta-data-source>myprojectname-mysql-jdbc-jta-resource</jta-data-source>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <properties>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
        </properties>
    </persistence-unit>
</persistence>

和我的实体经理:

public abstract class BaseDao {

    @PersistenceContext(unitName = "myprojectname-persistence-unit", type = PersistenceContextType.EXTENDED)
    protected EntityManager em;

}

和我的resources.xml:

<Resource id="myprojectname-mysql-jdbc-jta-resource" type="javax.sql.DataSource">
    jtaManaged = true
    DataSourceCreator = tomcat
    validationQuery = SELECT 1
    initialSize = 2
    removeAbandoned = true
    removeAbandonedTimeout = 120
    driverClassName = com.mysql.jdbc.Driver
    url = jdbc:mysql://localhost/myprojectdb
    UserName = usernamestring
    password = passwordstring
    testOnBorrow = true
</Resource>

注意在我的pom.xml中,我删除了测试,因为我希望它们不仅用于测试,还用于产品。

我将openejb-core工件设置为<version>4.7.2</version> AND <scope>provided</scope>,因为我在tomEE-home(v1.7.2)/ lib目录中找到了此版本。如果tomEE版本不同,则openejb-core的版本是不同的。

编辑:pom.xml中不需要openEJB javaee-api,所以我对它进行了评论。 此外,EclipseLink 2.6.2并不完全兼容,因此我将其更改为2.4.2。 请参阅:Why is my EclipseLink Query Results Cache NOT working

答案 2 :(得分:0)

正如Hirofumi Okino所建议的那样,使用以下依赖项:

[string map {" " ""} $a];

并且该示例将在没有7.0-SNAPSHOT

的情况下工作