0

I have a web-project with the following setup:

  • Wildfly 9.0
  • JSF 2.2
  • EJB 3.2
  • Hibernate 4.2.21

I want to do a simple method call from a ManagedBean to an injected EJB. Hibernate is configured as EntityManager for the EJB.

The view:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:html xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions">
<h:head>

</h:head>
<h:body>

    <h:form>
        <h:inputText value="#{courseController.course.room}"/>

        <h:commandButton 
            value="save" action="#{courseController.addCourse}"/>    
    </h:form>

</h:body>
</h:html>

The ManagedBean for the view:

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;

import de.uulm.vs.increase.teachinput.ejb.CourseEJB;
import de.uulm.vs.increase.teachinput.model.Course;

@Named(value="courseController")
@ViewScoped
public class CourseController implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @EJB
    private CourseEJB courseEJB;

    private Course course = new Course();

    public String addCourse()
    {
        courseEJB.addCourse(course);
        return "course.xhtml";
    }

    public Course getCourse()
    {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

}

The EJB:

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import de.uulm.vs.increase.teachinput.model.Course;

@Stateless(mappedName = "courseEJB")
@LocalBean
public class CourseEJB implements CourseEJBLocal {

    @PersistenceContext(unitName = "teach-input")
    EntityManager em;

    public CourseEJB() {

    }

    public Course addCourse(Course c)
    {
        em.persist(c);
        return c;
    }

}

The EJB interface:

import javax.ejb.Local;

import de.uulm.vs.increase.teachinput.model.Course;

@Local
public interface CourseEJBLocal {

    public Course addCourse(Course c);
}

persistence.xml:

<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="teach-input" transaction-type="JTA">
        <description>
            Persistence unit for the teach-input project.
        </description>

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>

            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"></property>
            <property name="hibernate.connection.username" value="user"></property>
            <property name="hibernate.connection.password" value="pw"></property>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"></property>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />

        </properties>

    </persistence-unit>

</persistence>

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                           http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.1" bean-discovery-mode="all">
</beans>

I build a single war file via maven

pom.xml:

<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>groupid</groupId>
    <artifactId>artifactid</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>name</name>

    <properties>
        <hibernate.version>4.2.21.Final</hibernate.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <!-- EJB -->
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>javax.ejb-api</artifactId>
            <version>3.2</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSF -->

        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

        <!-- WELD -->
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.3.2.Final</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>finalname</finalName>
        <defaultGoal>install</defaultGoal>
        <pluginManagement>
            <plugins>
                <!-- WildFly plugin to deploy the application -->
                <plugin>
                    <groupId>org.wildfly.plugins</groupId>
                    <artifactId>wildfly-maven-plugin</artifactId>
                    <version>1.0.2.Final</version>
                    <configuration>
                        <filename>${project.build.finalName}.war</filename>
                        <hostname>127.0.0.1</hostname>
                        <port>9990</port>
                        <username>user</username>
                        <password>password</password>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>true</downloadJavadocs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

The server is deploying properly. When invoking the addCourse() method vie the web interface, the following Exception appears:

WFLYEJB0034: EJB Invocation failed on component CourseEJB for method public Course addCourse(Course): javax.ejb.EJBException: java.lang.NullPointerException

So it seems there is something going wrong with the injection of the EJB. I looked for it in the WildFly documentation, but the EJB configuration chapter is missing.

Thanks in advance for any help.

bauz
  • 125
  • 1
  • 12

2 Answers2

0
  1. remove the @LocalBean annotation from your EJB implementation,
  2. remove the mappedName attribute - do you need it at this example?
  3. Inject using the Interface, e.g @EJB CourseEJBLocal

Hope that helps

javapapo
  • 1,342
  • 14
  • 26
  • Followd the three steps above. NullPointerException is still there. – bauz Jan 07 '16 at 11:13
  • Ok weird, try the simplest solution. Remove the Interface (or disable the annotation) and just annotate your EJB @ Stateless public class CourseEJB, then just use @ EJB CourseEJB . Does this work? – javapapo Jan 07 '16 at 11:15
  • After removing the interface and annotating the bean, the following Error appears: Error injecting resource into CDI managed bean. Can't find a resource named java:comp/env/package.controller.CourseController/courseEJB Why is the name java:comp and not something like app/global/module? – bauz Jan 07 '16 at 11:18
  • Can you provide some details about your ear /war packaging? java:comp is a standard JNDI namespace – javapapo Jan 07 '16 at 11:25
  • It is a simple maven project that creates a single war file. No additional configuration so far. – bauz Jan 07 '16 at 11:42
  • another test, in this simple @Stateless EJB, remove the code that injects the persistent context. (by the way get rid of the constructor) – javapapo Jan 07 '16 at 12:27
  • Removed the EntityManager. Now, there's no exception so far. Seems like it has got something to do with the `@PersistenceContext` – bauz Jan 07 '16 at 15:06
  • By the way, having a short look on your pom there are some issues with your dependencies that could be related. If you are using the standard stuff (Hibernate (JPA2))/ EJB etc you dont need for example to include hibernate or something extra. There are already API based dependencies to use. See [here](http://www.adam-bien.com/roller/abien/entry/a_list_of_java_ee) – javapapo Jan 07 '16 at 15:08
  • Thanks for the hint. Changed it accordingly, Exception still there. – bauz Jan 07 '16 at 15:27
0

You are mixing CDI-@Named with JSF-Scopes:

Use either

import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;


@Named
@ConversationScoped
public class CourseController implements Serializable {

or

import javax.faces.bean.ViewScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean
@ViewScoped
public class CourseController implements Serializable {
knoe
  • 644
  • 1
  • 5
  • 16