0

我正在使用GlassFish 3 开始 Java EE 6 平台进行企业计算课程:从新手到专业

上一章是关于 EJB 的,实际上我发现它很难理解。

为了更好地理解 EJB,我一直在尝试运行本书的示例代码之一,但是每当我调用 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 设置为各自的目录,但没有任何效果。我什至找到了一个链接,指向有人在教科书的同一章(https://getsatisfaction.com/javaee6/topics/yet_another_chapter_6_ejb_problem)看到它在 StackOverflow 中引用后,但在那里找不到任何东西(没有老实说,我不明白)

4

1 回答 1

0

这不是 maven 问题,您应该在 java SE 环境中设置 ejb 容器以使用 EJB。

尝试这样的事情

EJBContainer.createEJBContainer()

您可以在http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/embeddedContainerDemo/EmbeddedContainerDemo.html获得完整的示例

于 2013-08-06T11:17:17.050 回答