【问题标题】:After compiling correctly, a Maven project don´t execute正确编译后,Maven项目不执行
【发布时间】:2012-12-10 20:45:19
【问题描述】:

我正在按照“使用 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>

而操作实体的主类是:

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

【问题讨论】:

    标签: jakarta-ee maven jpa


    【解决方案1】:

    意思是

    org.eclipse.persistence.jpa.PersistenceProvider  
    

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

    【讨论】:

    • @user1881983 本地仓库在哪里? O_o 你的 jar 应该包含 javax.persistence artifact
    • 但持久性文件存在于目标/类路径中。作为编译结果,我的目标目录的结构是:TARGET/CLASSES/META-INF/persistance.xml 和 TARGET/CLASSES/COM/APRESS/JAVAEE6/CHAPTER02/main.class 和 book.class。加上 eclipselink 存储库位于 .m2 下,其路径为 C:\Users\my_name\.m2
    【解决方案2】:

    我们需要将classpathScope参数值,default value runtime改为compile

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

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 2019-08-16
      • 2021-06-25
      • 2016-10-27
      • 2016-07-15
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多