我使用quickstart原型生成了一个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>
正如你所注意到的,我和德比一起工作。 “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类中使用以下anotatations:
@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
非常感谢你, 问候。
答案 0 :(得分:7)
我认为问题是“id = null”。但是,当我手动设置Id(例如123)时,我得到相同的错误消息。
不,您在使用Id
时不应设置GeneratedValue
。
任何人都有解决此错误的方法吗?
问题的根本原因是BOOK
表未生成(因此EclipseLink无法正确映射Book
实体,因此无法将其识别为实体)。
这是因为persistence.xml
中列出的类的完全限定名称与实际FQN(com.apress.javaee6.chapter02.Book
vs com.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.DatabaseException Internal Exception: java.sql.SQLException: Table/View 'SEQUENCE' already exists in Schema 'APP'. Error Code: -1 Call: 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应报告该表丢失。
答案 1 :(得分:0)
我自己没有使用过EclipseLink,所以我只能猜测,但让我感到惊讶的是你的类没有id属性的setter。
根据JPA规范,我不知道这是否合法,但我以前从未遇到过这种情况。在我所知道的所有JPA应用程序中,所有非瞬态属性都有公共getter和setter。 (当然其中一些不应该手动使用,比如id属性,但Persistence Provider需要它们)。所以尝试添加一个setId()方法,我们将从那里开始......
答案 2 :(得分:0)
我在Eclipselink上遇到了很多这样的错误。 我找到的唯一答案是这个,一个开放的bug:
简而言之,这个bug来自EclipseLink +(osGI)联合使用,当重建jar时,保留指向“旧”实体的指针而不是使用新指针,因此实体未知的错误。我的解决方案是重启IDE,清理,构建,重新启动服务器,安装jar / war,运行..
不知道这是不是你的问题,但可能是......