0

Beginning Java EE 6 Platform with GlassFish 3: From Novice to Professionalを使用して、エンタープライズ コンピューティングのコースを行っています。

前の章は EJB に関するものでしたが、実際には理解するのが非常に難しいと感じました。

EJB をよりよく理解するために、本のサンプル コードの 1 つを実行しようとしましたが、EJB を呼び出すたびに NullPointerException が発生します。

NetBeans 7.3.1 と、NetBeans で提供される Maven および Glassfish バージョンを使用しています (Maven は 3.0.5、Glassfish は 4.0 のようです)。

これは、メイン クラスのコードです。

package org.beginningee6.book.chapter06;

import javax.ejb.EJB;

/**
 * @author Antonio Goncalves
 *         APress Book - Beginning Java EE 6 with Glassfish
 *         --
 */
public class Main {

    // ======================================
    // =             Attributes             =
    // ======================================

    @EJB
    private static BookEJBRemote bookEJB;

    // ======================================
    // =           Public Methods           =
    // ======================================

    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 series created by Douglas Adams.");
        book.setIsbn("1-84023-742-2");
        book.setNbOfPage(354);
        book.setIllustrations(false);

        book = bookEJB.createBook(book);
        System.out.println("### Book created : " + book);

        book.setTitle("H2G2");
        book = bookEJB.updateBook(book);
        System.out.println("### Book updated : " + book);
        System.out.println("Execution succeeded");

    }
}

persistence.xml は次のとおりです。

<?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="chapter06PU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <!--<jta-data-source>jdbc/__default</jta-data-source>-->
        <jta-data-source>jdbc/chapter06DS</jta-data-source>
        <class>org.beginningee6.book.chapter06.Book</class>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

これが pom.xml です。

<?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>chapter06</artifactId>
    <packaging>jar</packaging>
    <version>2.0</version>
    <name>Week5</name>

    <parent>
        <groupId>org.beginningee6.book</groupId>
        <artifactId>chapters</artifactId>
        <version>2.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>${javax.persistence-version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>${eclipselink-version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>${glassfish-version}</version>
        </dependency>
    </dependencies>
    <!--To avoid multiple modules with Maven, here is what you need to manually do (it's not nice, but it works)
    1) Comment the following section (maven-jar-plugin), package the jar, and deploy to GlassFish
    2) Uncomment the following section, package the jar and run the Main class with app client --> 
    <build>
<!--        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${plugin-jar-version}</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.beginningee6.book.chapter06.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>-->
    </build>

</project>

次のエラーが表示されます。

Exception in thread "main" java.lang.NullPointerException

この行で:

book = bookEJB.updateBook(book);

どうやらMavenがこのエラーを出しているようです:

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed.

Maven のデバッグ オプションを有効にした後、エラーが発生する前の最後のコマンド ラインは次のとおりです。

Executing command line: C:\Program Files (x86)\Java\jdk1.7.0_21\bin\java.exe -classpath C:\Users\Manuel_Laptop\Desktop\Week5\COIT20227LabSolWeek5\Week5\target\classes;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\javax.persistence\2.0.0\javax.persistence-2.0.0.jar;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\eclipselink\2.0.1\eclipselink-2.0.1.jar;C:\Users\Manuel_Laptop\.m2\repository\org\glassfish\extras\glassfish-embedded-all\3.0.1-b19\glassfish-embedded-all-3.0.1-b19.jar org.beginningee6.book.chapter06.Main

いくつかのコースのヒントといくつかのフォーラムを通過した後、JAVA_HOME と M2_HOME をそれぞれのディレクトリに設定しましたが、何も機能しませんでした。StackOverflowで参照されているのを見た後、教科書の同じ章で問題を抱えている人へのリンクを見つけました(https://getsatisfaction.com/javaee6/topics/yet_another_chapter_6_ejb_problem )が、そこには何も見つかりませんでした(didn'正直に言うとわかりません)

4

1 に答える 1

0

Maven の問題ではありません。EJB を使用するには、Java SE 環境で ejb コンテナーをセットアップする必要があります。

このようなものを試してください

EJBContainer.createEJBContainer()

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/embeddedContainerDemo/EmbeddedContainerDemo.htmlで完全な例を取得できます。

于 2013-08-06T11:17:17.050 に答える