TOMEE jpa-eclipselink 示例不起作用

TOMEE jpa-eclipselink example does not working

TOMEE jpa-eclipselink 示例:

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

对我不起作用

EJBContainer.createEJBContainer(p) 单元测试 class (MoviesTest) 内失败,出现以下异常:

...
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> 

因为第一个不会构建。

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

此致

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

Since:EJB 3.2

所以这个方法只在 JEE7 中引入,因为 JEE6 使用 EJB 3.1。

您使用的 eclipselink-version 似乎需要 JEE7 实现(这就是他们将其放入 dependency-list 的原因)并且不能与 JEE6 一起使用。

如果您无法使用 JavaEE 6.0.6 进行构建, 我为您提供了我可以使用 eclipselink、TomEE 1.7.2、Mysql.

构建和 运行 的设置

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

下面是我的 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><scope>provided</scope>,因为我在 tomEE-home(v1.7.2)/lib 目录中找到了这个版本。 openejb-core的版本是不同的,如果tomEE版本不同。

EDIT:pom.xml 中不需要 openEJB javaee-api,因此我将其注释掉。 此外,EclipseLink 2.6.2 不完全兼容,因此我将其更改为 2.4.2。 参见:

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

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

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

并且该示例在没有 7.0-SNAPSHOT 的情况下也可以工作