InvocationTargetException:javax/persistence/Persistence [英] InvocationTargetException: javax/persistence/Persistence

查看:86
本文介绍了InvocationTargetException:javax/persistence/Persistence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遵循《用GlassFish 3开始Java EE 6平台》一书第2章中的示例.我正在Windows中使用cmd中的EclipseLink,Derby和Maven.我真的无法解决这个问题,将不胜感激!

Im trying to follow the example in chapter 2 of the book Beginning Java EE 6 Platform with GlassFish 3. I am using EclipseLink, Derby and Maven from cmd in Windows. I really can't figure this out and would be grateful for some help!

Main.java类中的此行EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter02PU");会产生以下错误:

This line EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter02PU"); in my Main.java class generates the following error:

    [INFO] Error stacktraces are turned on.
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Chapter 02 - JPA 0.1
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ chapter02 ---
    [debug] execute contextualize
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 1 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ chapter02 ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO] 
    [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ chapter02 >>>
    [INFO] 
    [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ chapter02 <<<
    [INFO] 
    [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ chapter02 ---
    [WARNING] 
    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:601)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
        at java.lang.Thread.run(Thread.java:722)
    Caused by: java.lang.NoClassDefFoundError: javax/persistence/Persistence
        at org.beginningee6.book.chapter02.Main.main(Main.java:22)
        ... 6 more
    Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 7 more
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 8.390s
    [INFO] Finished at: Tue Mar 19 23:13:36 CET 2013
    [INFO] Final Memory: 8M/105M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project chapter02: An exception occured while executing the Java class. null: InvocationTargetException: javax/persistence/Persistence: javax.persistence.Persistence -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project chapter02: An exception occured while executing the Java class. null
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        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:601)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    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:352)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        ... 19 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:601)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
        at java.lang.Thread.run(Thread.java:722)
    Caused by: java.lang.NoClassDefFoundError: javax/persistence/Persistence
        at org.beginningee6.book.chapter02.Main.main(Main.java:22)
        ... 6 more
    Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 7 more
    [ERROR] 
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR] 
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException


这是我的Main.java类:


This is my Main.java class:

package org.beginningee6.book.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) {

        Book book = new Book();
        book.setTitle("The Hitchhicker'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);

        // Get en entity manager and a transaction
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter02PU");
        //EntityManager em = emf.createEntityManager();

        //Persist the book to the database
        /*EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.persist(book);
        tx.commit();

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

}


这是我的Book.java类:


This is my Book.java class:

package org.beginningee6.book.chapter02;


import javax.persistence.*;

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

    public Book() {

    }

//Plus getters and setter.


这是我的persistence.xml文件:


This is my persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>org.beginningee6.book.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.EmbeddedDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:chapter02DB;create=true"/> 
            <!-- <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>


这是我的pom.xml文件:


This i my pom.xml file:

<?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>org.beginningee6.book</groupId>
        <artifactId>chapter02</artifactId>
        <version>0.1</version>
        <name>Chapter 02 - JPA</name>    

        <dependencies>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.0.0</version>
                <scope>provided</scope>
            </dependency> 
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>eclipselink</artifactId>
                <version>2.4.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.derby</groupId>
                <artifactId>derbyclient</artifactId>
                <version>10.8.2.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.derby</groupId>
                <artifactId>derby</artifactId>
                <version>10.8.2.2</version>
            </dependency>
        </dependencies>

</project>


这是我在Eclipse中的文件结构:


And this is my file structure in Eclipse:

JavaEEAPress\src\main\java\org\beginningee6\book\chapter02\Book.java
JavaEEAPress\src\main\java\org\beginningee6\book\chapter02\Main.java
JavaEEAPress\src\main\resources\META-INF\persistence.xml
JavaEEAPress\pom.xml

推荐答案

您在持久性库中指定了provided范围.

You specified a provided scope on your persistence libraries.

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.4.0</version>
    <scope>provided</scope>
</dependency>

这基本上告诉Maven,出于编译目的,请使用这些库,但不要在最终的工件中包含它们,因为它们最终将通过其他方式提供".我确定您所遵循的教程是通过这种方式定义它们的,因为期望您将代码部署到要执行的应用服务器上.在这种情况下,确实提供了那些库.

That basically tells Maven that 'for purposes of compiling, use these libraries, but don't include them in my final artifact because they will eventually be provided by some other means'. I'm sure the tutorial you are following defined them this way because it is expected that you will be deploying your code to an application server to be executed. In such a scenario, those libraries are indeed provided.

但是,在这种情况下,您正在运行一个独立的Java应用程序,并且需要明确包含这些库.最简单的解决方法是将org.eclipse.persistence:javax.persistenceorg.eclipse.persistence:eclipselink的范围从provided更改为runtime.但是仅当您决不打算将示例部署到应用程序服务器时.更好的选择是为运行独立代码定义配置文件:

But, in this case, you are running a standalone Java application, and you need to include those libraries explicitly. The simplest fix would be to change the scope, for org.eclipse.persistence:javax.persistence and org.eclipse.persistence:eclipselink from provided to runtime. But only if you never intend to deploy your examples to an application server. A better option would be to define a profile for running standalone code:

<profiles>
    <profile>
        <id>standalone</id>
        <dependencies>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>eclipselink</artifactId>
                <version>2.4.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

通过使用配置文件选项执行Maven可以激活以下哪项:

Which would be activated by executing Maven with a profile option:

MVN全新安装-独立

mvn clean install -Pstandalone

这篇关于InvocationTargetException:javax/persistence/Persistence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆