0

I can't for the life of me figure out whats wrong with the code. I have spent some time trying to debug the issue. When i fill out the add employee form only null values are being created in the form. At this point i am stuck and need some help to get to the solution.

EmpController.java

package ejb605.controller.com;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

import ejb605.assignment2.com.EmployeeManager;
import entity.Employee;

@ManagedBean
@SessionScoped

public class EmpController implements Serializable{



    @EJB
    EmployeeManager em; 

    private List<Employee> list = new ArrayList<>();
    private Employee emp = new Employee();
    private String empSearchID; 

    public List<Employee> getList() {
        list = em.getAllEmployees();
        return list;
    }


    public String getEmpSearchID() {
        return empSearchID;
    }


    public void setEmpSearchID(String empSearchID) {
        this.empSearchID = empSearchID;
    }


    public Employee getEmp() {
        return emp;
    }


    public void setEmp(Employee emp) {
        this.emp = emp;
    }


    public String Search(){
        this.setEmp(em.getEmployee(Integer.parseInt(this.empSearchID))); 
        return "EmployeeSearchDetails"; 
    }

    public String AddEmp(){
        emp = em.addEmployee(this.emp);
        list = em.getAllEmployees();
        return "NewEmployee";   
    }


}

Employee.java JPA

package entity;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;


/**
 * The persistent class for the employees database table.
 * 
 */
@Entity
@Table(name="employees")
@NamedQuery(name="findAllEmployees", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="department_id")
    private int departmentId;

    private String email;

    @Column(name="first_name")
    private String firstName;

    @Temporal(TemporalType.DATE)
    @Column(name="hire_date")
    private Date hireDate;

    @Column(name="last_name")
    private String lastName;

    @Column(name="manager_id")
    private int managerId;

    private String phone;

    public Employee() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getDepartmentId() {
        return this.departmentId;
    }

    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public Date getHireDate() {
        return this.hireDate;
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getManagerId() {
        return this.managerId;
    }

    public void setManagerId(int managerId) {
        this.managerId = managerId;
    }

    public String getPhone() {
        return this.phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

package ejb605.assignment2.com;

import java.util.List;

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

import entity.Employee;

/**
 * Session Bean implementation class EmployeeManager
 */
@Stateless
@LocalBean
public class EmployeeManager implements EmployeeManagerLocal {
    @PersistenceContext(name="EmployeeJPA")
    EntityManager em;

    public EmployeeManager(){

    }

    public List<Employee> getAllEmployees() {
        Query q = em.createNamedQuery("findAllEmployees",Employee.class); 
        return q.getResultList();
    }


    public Employee getEmployee(Integer id){
        return em.find(Employee.class, id); 
    }

    public void updateEmployee(Employee e){
        Employee emp = getEmployee(e.getId());
        emp.setFirstName(e.getFirstName());
        emp.setLastName(e.getLastName());
        emp.setDepartmentId(e.getDepartmentId());
        emp.setEmail(e.getEmail());
        emp.setHireDate(e.getHireDate());
        emp.setManagerId(e.getManagerId());
        emp.setPhone(e.getPhone());

        em.persist(emp);
        em.flush();
    }

    @Override
    public Employee addEmployee(Employee e) {
        em.persist(e);
        em.flush();

        return e;
    }

    public void deleteEmployee(Integer id) {
        Employee e = em.find(Employee.class, id);
        if ( e == null ) {
            System.err.println("Error deleteing employee "  + id);
        }else {
            em.remove(e);
            em.flush();
        }
    }

}

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:outputText value="#{employeeSearchController}"></h:outputText>
 <f:loadBundle basename="resources.application" var="msg"/>

<head>
    <title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>
<body>
<CENTER><h1><b>Employee Details</b></h1></CENTER>

    <h4 style="color:red">${error}</h4>
          <h:form>
    <fieldset style="width:=300px">
    <legend>Find Employee</legend>
        <table>
            <tr>
                <td>Employee ID:</td>
                <td><h:inputText id="i1" value="#{empController.empSearchID}" label="Employee Id"></h:inputText></td>
            </tr>
        </table>
    </fieldset>
        <h:commandButton value="Search" action="#{empController.Search}" />
      </h:form>

    <legend>Add Employee</legend>
      <h:form>
        <table> 
            <tr> 
                <td>Employee ID (Key)*:</td>
                <td><h:inputText id="i2" value="#{empController.emp.id}" label="EmployeeId"></h:inputText></td>
            </tr>
            <tr>
                <td>First Name *:</td>
                <td><h:inputText id="i3" value="#{empController.emp.firstName}" label="FirstName"></h:inputText></td>
            </tr>
            <tr> 
                <td>Last Name: *:</td>
                <td><h:inputText id="i4" value="#{empController.emp.lastName}" label="LastName"></h:inputText></td>
            </tr>
            <tr> 
                <td> Email: </td>
                <td><h:inputText id="i5" value="#{empController.emp.email}" label="email"></h:inputText></td>
            </tr>
            <tr>
                <td> Phone: </td>
                <td><h:inputText id="i6" value="#{empController.emp.phone}" label="phoneNumber"></h:inputText></td>
            </tr>
            <tr> 
                <td>Hire Date(MM/DD/yyyy):</td>
                <td><h:inputText id="i7" value="#{empController.emp.hireDate}" label="hireDate"></h:inputText></td>
            </tr>
            <tr>
                <td>Manager Id:</td>
                <td><h:inputText id="i8" value="#{empController.emp.managerId}" label="Manager ID"></h:inputText></td>
            </tr>
            <tr>
                <td>Department Id:</td>
                <td><h:inputText id="i9" value="#{empController.emp.departmentId}" label="DepartmentID"></h:inputText></td>
            </tr>
        </table>
        <h:commandButton value="Add" immediate="true" action="#{empController.AddEmp}" />       
              </h:form>

</body>
<h:commandButton value="update Employee" immediate="true" action="#{empController.updateEmp}" />        

</html>

NewEmployee.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<f:loadBundle basename="resources.application" var="msg"/>

<head>
    <title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>
<h:body>
<h:dataTable value="#{empController.list}" var="e" border="2">
<h:column>
    <f:facet name="header">
        <h:outputText value="First Name"/>
    </f:facet>
        <h:outputText value="#{e.firstName}"/>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Last Name"/>
    </f:facet>
        <h:outputText value="#{e.lastName}"/>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Email"/>
    </f:facet>
        <h:outputText value="#{e.email}"/>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Phone"/>
    </f:facet>
        <h:outputText value="#{e.phone}"/>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Hire Date"/>
    </f:facet>
        <h:outputText value="#{e.hireDate}"/>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Manager ID"/>
    </f:facet>
        <h:outputText value="#{e.managerId}"/>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Department ID"/>
    </f:facet>
        <h:outputText value="#{e.departmentId}"/>
</h:column>
    </h:dataTable>
 </h:body>
</html>
Rez88
  • 57
  • 1
  • 10
  • 3
    Why do people always insist upon using verbose session scoped managed beans quite unnecessarily? Do you really need a session scoped managed bean in the episode you described above? It was about a decade ago during the JSF 1.x era. Things have been changing remarkably since then. No it is not related to the said problem - it is completely off-topic. – Tiny Apr 11 '15 at 15:51
  • When i remove the immediate=true it doesn't redirect to the JSF page. I think this is where the problem lies. – Rez88 Apr 11 '15 at 16:37
  • What do you get? "*Doesn't redirect*" is not very useful. 1) I would use individual properties attributing an employee in the managed bean rather than an instance of the entity `Employee` itself. 2) You are using a plain HTML `` tag instead of using the standard JSF `` tag. As such, you will encounter problems loading JavaScript at a certain time. 3) You misplaced an `` outside the `` tag. 4) Also the `
    ` tag (which in turn, is irritatingly capitalized) was deprecated a long time ago. You do not even need to remember it anymore now.
    – Tiny Apr 11 '15 at 17:42
  • You are also using the plain HTML `` tag rather than using the standard JSF `` tag. – Tiny Apr 11 '15 at 17:48
  • Thank you ill get those fixed. When i search for an employee in the index.xhtml form i get the correct results from the managed bean. However when i try to add an employee i get redirected but the values are null in the form. I was wondering if this can be attributed to the scope of the session? – Rez88 Apr 11 '15 at 17:49
  • No, session scope is not responsible in this case. What do you get, if for example, you bind an `` to a simple property in the managed bean ignoring the whole mess of the other tags such as `` `private String property;` with a getter and a setter? Do you see the correct input value being set to the property of the managed bean? – Tiny Apr 11 '15 at 17:57
  • Yes i am trying that. So empController.firstName instead of using the JPA entity. I created a firstName property in my managedbeans and in the addEmp method i am doing this this.setFirstName(this.firstName) emp = em.addEmployee(this.emp); list = em.getAllEmployees(); return "NewEmployee"; Still showing null values in the form. – Rez88 Apr 11 '15 at 18:03
  • I edited. The form is being filled now but it is being filled with different values regardless of what is entered in the input. – Rez88 Apr 11 '15 at 18:09
  • Try preparing a simple test case having only a few fields (one or two) in the managed bean being bound to `` excluding JPA/EJB in its entirely, since this is completely unrelated to JPA/EJB which goes to another layer (and this time also do not use wonky method names like `AddEmp()` and `Search()` beginning with a capital letter. They should be `addEmp()` and `search()` instead). It is difficult to see what is wrong in the current code snippets - apparently it is not reproducible on a blank playground project having least possible dependencies and resources. – Tiny Apr 11 '15 at 18:27
  • I got it working thanks for your help. Just need to parse the date. – Rez88 Apr 11 '15 at 19:10
  • possible duplicate of [Nested Forms Command Button not nagivating to JSF](http://stackoverflow.com/questions/29580430/nested-forms-command-button-not-nagivating-to-jsf) – 0x5a4d Apr 12 '15 at 05:44
  • @0x5a4d: check the code. There are no nested forms afaics – Kukeltje Apr 12 '15 at 09:02
  • 1 problem 2 questions, same examples – 0x5a4d Apr 12 '15 at 09:03

0 Answers0