Hi everybody,
I generated a maven project with the quickstart archetype. So I obtained the following project structure:
|-- src
| |-- main
| | |-- java
| | |-- resources
| |-- test
| | |-- java
| | |-- resources
|-- pom.xml
I modified the "pom.xml" file :
<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>
As you noticed, I work with derby. The "src/main/resources/META-INF/persistence.xml" is:
<?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>
Then, I created a "Book" Class:
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();
}
}
And, of course, the "Main class":
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();
}
}
I start the Derby server:
./derby/bin/startNetworkServer
The problem is when I run the main class:
mvn exec:java -Dexec.mainClass="com.apress.javaee6.Main"
I get the following error:
[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.
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. I also tried to use the following anotatations in the Book class:
@GeneratedValue(strategy=GenerationType.AUTO)
or
@GeneratedValue(strategy=GenerationType.IDENTITY)
Anybody has the solution to this error?
PS: Here is the exec command result with error stacktrace turned on:
+ 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
Thak you very much, Regards.