title: Fields of injected bean are null when trying to set them via JSF
body: <p>I am following a book about JEE and trying to implement a simple JSF example. I want to register and persist a <em>Customer</em> via a JSF page and a controller. </p>
  
  <p>The problem is that the attributes ot the injected <em>Customer</em> in the controller are always null, no matter what I type into the JSF registration form. </p>
  
  <p>The registerController.persist() method is getting executed on form submission, though.</p>
  
  <p>When I submit the form with the email "user@email.com" for example, I would expect the injected Customer in the controller to have an email, but it is always null. Perhaps somebody can give me a suggestion where the problem lies.</p>
  
  <p>The JSF page:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:f="http://xmlns.jcp.org/jsf/core"&gt;
  &lt;h:head&gt;
      &lt;title&gt;
          &lt;h:outputText value="Onlineshop"/&gt;
      &lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
      &lt;h:form&gt;
          &lt;h:panelGrid columns="2"&gt;
              &lt;f:facet name="header"&gt;
                  &lt;h:outputText value="Registrieren"/&gt;
              &lt;/f:facet&gt;
              &lt;h:outputLabel value="E-Mail:"/&gt;
              &lt;h:inputText value="#{registerController.customer.email}"&gt;&lt;/h:inputText&gt;
              &lt;h:outputLabel value="Kennwort:"/&gt;
              &lt;h:inputSecret value="#{registerController.customer.password}"&gt;&lt;/h:inputSecret&gt;
              &lt;h:commandButton action="#{registerController.persist}" value="Registrieren"/&gt;
          &lt;/h:panelGrid&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The Controller: </p>
  
  <pre><code>package de.java2enterprise.onlineshop;
  
  import de.java2enterprise.onlineshop.model.Customer;
  
  import javax.annotation.Resource;
  import javax.faces.bean.RequestScoped;
  import javax.inject.Inject;
  import javax.inject.Named;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  import javax.persistence.PersistenceUnit;
  import javax.transaction.UserTransaction;
  
  @Named
  @RequestScoped
  public class RegisterController {
  
      @PersistenceUnit()
      private EntityManagerFactory emf;
  
      @Resource
      private UserTransaction ut;
  
      @Inject
      private Customer customer;
  
      public Customer getCustomer() {
          return customer;
      }
  
      public void setCustomer(Customer customer) {
          this.customer = customer;
      }
  
      public String persist() {
          try {
              ut.begin();
              emf.createEntityManager().persist(customer);
              ut.commit();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return "/register.xhtml";
      }
  }
  </code></pre>
  
  <p>And the Customer:</p>
  
  <pre><code>package de.java2enterprise.onlineshop.model;
  
  import java.io.Serializable;
  import java.util.HashSet;
  import java.util.Set;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  import javax.persistence.SequenceGenerator;
  import javax.persistence.Table;
  
  /**
   * 
   * @author Alexander Salvanos
   *
   * The persistent class for the CUSTOMER database table.
   * 
   */
  @Entity
  @Table(schema="ONLINESHOP", name="CUSTOMER")
  @NamedQuery(
          name="Customer.findAll", 
          query="SELECT c FROM Customer c")
  public class Customer implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @SequenceGenerator(
              name="CUSTOMER_ID_GENERATOR", 
              sequenceName="SEQ_CUSTOMER",
              schema="ONLINESHOP",
              allocationSize=1,
              initialValue=1)
      @GeneratedValue(
              strategy=GenerationType.SEQUENCE, 
              generator="CUSTOMER_ID_GENERATOR")
      private Long id;
  
      private String email;
  
      private String password;
  
      //bi-directional many-to-one association to Item
      @OneToMany(mappedBy="seller")
      private Set&lt;Item&gt; offers;
  
      //bi-directional many-to-one association to Item
      @OneToMany(mappedBy="buyer")
      private Set&lt;Item&gt; purchases;
  
      public Customer() {
      }
  
      public Long getId() {
          return this.id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getEmail() {
          return this.email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getPassword() {
          return this.password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public Set&lt;Item&gt; getOffers() {
          return this.offers;
      }
  
      public void setOffers(Set&lt;Item&gt; offers) {
          this.offers = offers;
      }
  
      public Item addOffer(Item offer) {
          Set&lt;Item&gt; offers = getOffers();
          if(offers == null) {
              offers = new HashSet&lt;Item&gt;();
          }
          offers.add(offer);
          offer.setSeller(this);
  
          return offer;
      }
  
      public Item removeOffer(Item offer) {
          getOffers().remove(offer);
          offer.setSeller(null);
  
          return offer;
      }
  
      public Set&lt;Item&gt; getPurchases() {
          return this.purchases;
      }
  
      public void setPurchases(Set&lt;Item&gt; purchases) {
          this.purchases = purchases;
      }
  
      public Item addPurchase(Item purchase) {
          Set&lt;Item&gt; purchases = getPurchases();
          if(purchases == null) {
              purchases = new HashSet&lt;Item&gt;();
          }
          purchases.add(purchase);
          purchase.setBuyer(this);
          return purchase;
      }   
  
      public Item removePurchase(Item purchase) {
          getPurchases().remove(purchase);
          purchase.setBuyer(null);
  
          return purchase;
      }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((id == null) ? 0 : id.hashCode());
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj) {
              return true;
          }
          if (obj == null) {
              return false;
          }
          if (!(obj instanceof Customer)) {
              return false;
          }
          Customer other = (Customer) obj;
          if (id == null) {
              if (other.id != null) {
                  return false;
              }
          } else if (!id.equals(other.id)) {
              return false;
          }
          return true;
      }
  
      public String toString() {
          return id + "-" + email + "-" + password;
      }
  }
  </code></pre>