maven 项目中来自数据库的 EclipseLink select 实体

EclipseLink select entity from the database in a maven project

如何在 Maven 项目中使用 EclipseLink 查询数据库实体?我不断收到错误消息,指出未找到 class 定义。

以下查询将导致 NoClassDefFoundError 异常:

Query query = this.entityManager.createQuery("SELECT e FROM Edge e");

Edge class的实现如下(本项目启用了drop-and-create-tables):

@Entity
public class Edge {

    @Id
    @GeneratedValue
    int id;

    String edgeName;

    @OneToMany(cascade = CascadeType.PERSIST)
    private final List<Lane> lanes = new ArrayList<Lane>();

    public Edge() {
    }

    public Edge(String edgeName) {
        this.edgeName = edgeName;
    }

    ...
}

这是我正在使用的persistance.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="verplaatsingssysteem" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.jdbc.password" value=""/>
            <property name="eclipselink.jdbc.user" value="root"/>
            <property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/verplaatsingssysteem"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>

            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

抛出以下异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/libraries/antlr/runtime/Parser
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:198)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:173)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:125)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:109)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1326)
    at MovementSystem.MovementSystemDAO.getEdge(MovementSystemDAO.java:50)
    at Import.MainImport.createEdgeInDatabase(MainImport.java:99)
    at Import.MainImport.processImport(MainImport.java:53)
    at Import.MainImport.processFile(MainImport.java:41)
    at Import.MainImport.main(MainImport.java:30)
Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.internal.libraries.antlr.runtime.Parser
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 22 more

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>nl.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>3.0ea8</version>
        </dependency>
    </dependencies>

</project>

根据堆栈跟踪,我假设您缺少一个依赖项,可能是 org.eclipse.persistence.antlr。

我缺少 eclipselinkd 依赖项。你需要这样的东西:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
</dependency>

也可以看看这里: https://wiki.eclipse.org/EclipseLink/Maven