gpt4 book ai didi

java - Maven 和 "is not a known entity type"错误

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

我使用 quickstart 原型(prototype)生成了一个 Maven 项目。于是我得到了如下的项目结构:

|-- src
| |-- main
| | |-- java
| | |-- resources
| |-- test
| | |-- java
| | |-- resources
|-- pom.xml

我修改了“pom.xml”文件:

<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>com.apress.javaee6</groupId>
<artifactId>chapter02</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>chapter02</name>
<url>http://maven.apache.org</url>

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

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.6.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.6.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

如您所见,我使用 derby。 “src/main/resources/META-INF/persistence.xml”是:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.apress.javaee6.chapter02.Book</class>
<properties>
<property name="eclipselink.target-database" value="DERBY"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
</properties>
</persistence-unit>
</persistence>

然后,我创建了一个“Book”类:

package com.apress.javaee6;
import javax.persistence.*;

@Entity
@NamedQuery(name = "findAllBooks", query = "SELECT b FROM Book b")
public class Book {

// ======================================
// = Attributes =
// ======================================
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String title;
private Float price;
@Column(length = 2000)
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;

// ======================================
// = Constructors =
// ======================================

public Book() {
}

public Book(String title, Float price, String description, String isbn, Integer nbOfPage, Boolean illustrations) {
this.title = title;
this.price = price;
this.description = description;
this.isbn = isbn;
this.nbOfPage = nbOfPage;
this.illustrations = illustrations;
}

// ======================================
// = Getters & Setters =
// ======================================
public Long getId() {
return id;
}




public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Float getPrice() {
return price;
}

public void setPrice(Float price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public Integer getNbOfPage() {
return nbOfPage;
}

public void setNbOfPage(Integer nbOfPage) {
this.nbOfPage = nbOfPage;
}

public Boolean getIllustrations() {
return illustrations;
}

public void setIllustrations(Boolean illustrations) {
this.illustrations = illustrations;
}

// ======================================
// = hash, equals, toString =
// ======================================

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Book");
sb.append("{id=").append(id);
sb.append(", title='").append(title).append('\'');
sb.append(", price=").append(price);
sb.append(", description='").append(description).append('\'');
sb.append(", isbn='").append(isbn).append('\'');
sb.append(", nbOfPage=").append(nbOfPage);
sb.append(", illustrations=").append(illustrations);
sb.append('}');
return sb.toString();
}
}

当然还有“主类”:

package com.apress.javaee6;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;


public class Main {

public static void main(String[] args) {

// Creates an instance of book
Book book = new Book();
book.setId(1231L);
book.setTitle("The Hitchhiker's Guide to the Galaxy");
book.setPrice(12.5F);
book.setDescription("Science fiction comedy series created by Douglas Adams.");
book.setIsbn("1-84023-742-2");
book.setNbOfPage(354);
book.setIllustrations(false);

// Gets an entity manager and a transaction
EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter02PU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();

// Persists the book to the database
tx.begin();
em.persist(book);
tx.commit();

em.close();
emf.close();
}
}

我启动 Derby 服务器:

./derby/bin/startNetworkServer

问题是当我运行主类时:

mvn exec:java -Dexec.mainClass="com.apress.javaee6.Main"

我收到以下错误:

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

Object: Book{id=null, title='The Hitchhiker's Guide to the Galaxy', price=12.5, description='Science fiction comedy series created by Douglas Adams.', isbn='1-84023-742-2', nbOfPage=354, illustrations=false} is not a known entity type.

我认为问题出在“id=null”。但是,当我手动设置 Id 时(例如设置为 123),我会收到相同的错误消息。我还尝试在 Book 类中使用以下注释:

@GeneratedValue(strategy=GenerationType.AUTO)

@GeneratedValue(strategy=GenerationType.IDENTITY)

有人解决这个错误吗?

PS:这是打开错误堆栈跟踪的 exec 命令结果:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building chapter02
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[INFO] [exec:java {execution: default-cli}]
[EL Finest]: 2010-08-19 13:38:37.215--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Begin predeploying Persistence Unit chapter02PU; session file:/home/zakaria/Dev/chapter02/target/classes/_chapter02PU; state Initial; factoryCount 0
[EL Finest]: 2010-08-19 13:38:37.243--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=eclipselink.orm.throw.exceptions; default value=true
[EL Finer]: 2010-08-19 13:38:37.272--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Searching for default mapping file in file:/home/zakaria/Dev/chapter02/target/classes/
[EL Finer]: 2010-08-19 13:38:37.276--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Searching for default mapping file in file:/home/zakaria/Dev/chapter02/target/classes/
[EL Finest]: 2010-08-19 13:38:37.3--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--End predeploying Persistence Unit chapter02PU; session file:/home/zakaria/Dev/chapter02/target/classes/_chapter02PU; state Predeployed; factoryCount 0
[EL Finer]: 2010-08-19 13:38:37.3--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--JavaSECMPInitializer - transformer is null.
[EL Finest]: 2010-08-19 13:38:37.3--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Begin predeploying Persistence Unit chapter02PU; session file:/home/zakaria/Dev/chapter02/target/classes/_chapter02PU; state Predeployed; factoryCount 0
[EL Finest]: 2010-08-19 13:38:37.301--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--End predeploying Persistence Unit chapter02PU; session file:/home/zakaria/Dev/chapter02/target/classes/_chapter02PU; state Predeployed; factoryCount 1
[EL Finest]: 2010-08-19 13:38:37.309--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Begin deploying Persistence Unit chapter02PU; session file:/home/zakaria/Dev/chapter02/target/classes/_chapter02PU; state Predeployed; factoryCount 1
[EL Finer]: 2010-08-19 13:38:37.315--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Could not initialize Validation Factory. Encountered following exception: java.lang.NoClassDefFoundError: javax/validation/Validation
[EL Finest]: 2010-08-19 13:38:37.323--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=eclipselink.logging.level; value=FINEST; translated value=FINEST
[EL Finest]: 2010-08-19 13:38:37.323--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=eclipselink.logging.level; value=FINEST; translated value=FINEST
[EL Finest]: 2010-08-19 13:38:37.324--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=javax.persistence.jdbc.user; value=APP
[EL Finest]: 2010-08-19 13:38:37.324--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=javax.persistence.jdbc.password; value=xxxxxx
[EL Finest]: 2010-08-19 13:38:37.429--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=eclipselink.target-database; value=DERBY; translated value=org.eclipse.persistence.platform.database.DerbyPlatform
[EL Finest]: 2010-08-19 13:38:37.437--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=javax.persistence.jdbc.driver; value=org.apache.derby.jdbc.ClientDriver
[EL Finest]: 2010-08-19 13:38:37.438--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--property=javax.persistence.jdbc.url; value=jdbc:derby://localhost:1527/chapter02DB
[EL Info]: 2010-08-19 13:38:37.439--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--EclipseLink, version: Eclipse Persistence Services - 2.0.0.v20091127-r5931
[EL Config]: 2010-08-19 13:38:37.46--ServerSession(6419763)--Connection(22664464)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--connecting(DatabaseLogin(
platform=>DerbyPlatform
user name=> "APP"
datasource URL=> "jdbc:derby://localhost:1527/chapter02DB"
))
[EL Config]: 2010-08-19 13:38:37.857--ServerSession(6419763)--Connection(25782860)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Connected: jdbc:derby://localhost:1527/chapter02DB
User: APP
Database: Apache Derby Version: 10.6.1.0 - (938214)
Driver: Apache Derby Network Client JDBC Driver Version: 10.6.1.0 - (938214)
[EL Config]: 2010-08-19 13:38:37.857--ServerSession(6419763)--Connection(10594949)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--connecting(DatabaseLogin(
platform=>DerbyPlatform
user name=> "APP"
datasource URL=> "jdbc:derby://localhost:1527/chapter02DB"
))
[EL Config]: 2010-08-19 13:38:37.863--ServerSession(6419763)--Connection(29499086)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--Connected: jdbc:derby://localhost:1527/chapter02DB
User: APP
Database: Apache Derby Version: 10.6.1.0 - (938214)
Driver: Apache Derby Network Client JDBC Driver Version: 10.6.1.0 - (938214)
[EL Info]: 2010-08-19 13:38:37.885--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--file:/home/zakaria/Dev/chapter02/target/classes/_chapter02PU login successful
[EL Finest]: 2010-08-19 13:38:37.933--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--End deploying Persistence Unit chapter02PU; session file:/home/zakaria/Dev/chapter02/target/classes/_chapter02PU; state Deployed; factoryCount 1
[EL Finer]: 2010-08-19 13:38:38.004--ServerSession(6419763)--Thread(Thread[com.apress.javaee6.Main.main(),5,com.apress.javaee6.Main])--client acquired
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

Object: Book{id=1231, title='The Hitchhiker's Guide to the Galaxy', price=12.5, description='Science fiction comedy series created by Douglas Adams.', isbn='1-84023-742-2', nbOfPage=354, illustrations=false} is not a known entity type.
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:346)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:291)
at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.IllegalArgumentException: Object: Book{id=1231, title='The Hitchhiker's Guide to the Galaxy', price=12.5, description='Science fiction comedy series created by Douglas Adams.', isbn='1-84023-742-2', nbOfPage=354, illustrations=false} is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4147)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:368)
at com.apress.javaee6.Main.main(Main.java:30)
... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Aug 19 13:38:38 CEST 2010
[INFO] Final Memory: 8M/68M
[INFO] ------------------------------------------------------------------------
[EL Finer]: 2010-08-19 13:38:38.213--UnitOfWork(24128584)--Thread(Thread[Finalizer,8,system])--release unit of work
[EL Finer]: 2010-08-19 13:38:38.214--ClientSession(23817301)--Thread(Thread[Finalizer,8,system])--client released

非常感谢,问候。

最佳答案

I thought that the problem was the "id=null". But, when I set the Id manually (to 123 for example), I get the same error message.

不,您不应该在使用 GeneratedValue 时设置 Id

Anybody has the solution to this error?

问题的根本原因是 BOOK 表未生成(因此 EclipseLink 无法正确映射 Book 实体,然后无法识别它作为一个实体)。

发生这种情况是因为您的 persistence.xml 中列出的类的完全限定名称与实际的 FQN 不匹配(com.apress.javaee6.chapter02.Bookcom.apress.javaee6.Book).

因此,要么更改类的包,要么修复 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.apress.javaee6.Book</class>
<properties>
<property name="eclipselink.target-database" value="DERBY"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
</properties>
</persistence-unit>
</persistence>

然后运行你的 mvn exec:java -Dexec.mainClass="com.apress.javaee6.Main" 将会成功:

 mvn exec:java -Dexec.mainClass="com.apress.javaee6.Main" -e+ Error stacktraces are turned on.[INFO] Scanning for projects...[INFO] Searching repository for plugin with prefix: 'exec'.[INFO] ------------------------------------------------------------------------[INFO] Building Q3521044 - Maven and the “is not a known entity type” error[INFO]    task-segment: [exec:java][INFO] ------------------------------------------------------------------------[INFO] Preparing exec:java[INFO] No goals needed for project - skipping[INFO] [exec:java {execution: default-cli}][EL Info]: 2010-08-19 16:39:54.442--ServerSession(2698418)--EclipseLink, version: Eclipse Persistence Services - 2.0.0.v20091127-r5931[EL Info]: 2010-08-19 16:39:55.875--ServerSession(2698418)--file:/home/pascal/Projects/stackoverflow/Q3521044/target/classes/_chapter02PU login successful[EL Warning]: 2010-08-19 16:39:56.219--ServerSession(2698418)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseExceptionInternal Exception: java.sql.SQLException: Table/View 'SEQUENCE' already exists in Schema 'APP'.Error Code: -1Call: CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))Query: DataModifyQuery(sql="CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))")[EL Info]: 2010-08-19 16:39:56.574--ServerSession(2698418)--file:/home/pascal/Projects/stackoverflow/Q3521044/target/classes/_chapter02PU logout successful[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESSFUL[INFO] ------------------------------------------------------------------------[INFO] ------------------------------------------------------------------------[INFO] Total time: 11 seconds[INFO] Finished at: Thu Aug 19 16:39:56 CEST 2010[INFO] Final Memory: 17M/88M[INFO] ------------------------------------------------------------------------

但在我看来,错误报告的很差,EclipseLink 应该报告表丢失。

引用资料

  • JPA 1.0 规范
    • 6.2.1.6 映射文件、jar 文件、类、排除未列出的类

关于java - Maven 和 "is not a known entity type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3521044/

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