正确编译后,Maven项目无法执行

时间:2012-12-24 12:03:07

标签: java-ee maven jpa

我正在按照“使用Glassfish_3开始Java_EE_6”一书的教程来创建实体。我一步一步地按照步骤检查谷歌但是在执行时我仍然有错误。

因此,实体类是:

package com.apress.javaee6.chapter02;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;


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

    @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;

   //Here I put the constructor and getters and setters
   }

持久性单位是:

<?xml version="1.0" encoding="UTF-8"?>

<persistence 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_2_0.xsd" version="2.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="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>

和操纵实体的Main类是:

package com.apress.javaee6.chapter02;

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.setTitle("The Hitchhiker's Guide to the Galaxy");
        book.setPrice(12.5F);
        book.setDescription("Science fiction comedy book");
        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();

        // Persists the book to the database
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.persist(book);
        tx.commit();
        em.close();
        emf.close();
            }
}

maven项目的POM文件是:

<?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>com.apress.javaee6</groupId>
 <artifactId>chapter02</artifactId>
 <version>2.0</version>

<name>chapter02</name>
<properties>
    <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
    <eclipselink.version>2.4.0</eclipselink.version>
  </properties>
<repositories>
        <repository>
            <id>EclipseLink</id>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.8.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.8.1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>


<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.0</version>
          <configuration>
              <source>1.6</source>
              <target>1.6</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

当我编译时,一切都是正确的,但是执行main:

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

我得到了这个例外:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named chapter02PU

1 个答案:

答案 0 :(得分:0)

这意味着

org.eclipse.persistence.jpa.PersistenceProvider  

在运行时类路径中不存在。