gpt4 book ai didi

java - EclipseLink 从 Maven 项目中的数据库中选择实体

转载 作者:行者123 更新时间:2023-11-29 07:40:40 25 4
gpt4 key购买 nike

如何在 Maven 项目中使用 EclipseLink 查询数据库实体?我不断收到错误,提示未找到类定义。

以下查询将导致 NoClassDefFoundError 异常:

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

Edge 类的实现如下(该项目启用了 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.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.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$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.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>

最佳答案

我缺少 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

关于java - EclipseLink 从 Maven 项目中的数据库中选择实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com