title: Changes not reflected in JPA entities after updating in h:dataTable
body: <p>I am working with Eclipse and Glassfish 3.0. Pretty new to this technology although I have done similar things before. Very simple really got a datatable bound to a backing bean. Add methods and remove methods i have covered - the problem lies with the update method I am calling. I cannot seem to see the changes being picked up in the component (HtmlInputText) never mind passing the data back to the table.</p>
  
  <p>My code for the data table is below (and the jsf page)
      </p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;f:loadBundle basename="resources.application" var="msg"/&gt;
  
  &lt;head&gt;
      &lt;title&gt;&lt;h:outputText value="#{msg.welcomeTitle}" /&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;h:form id="mainform"&gt;
  
    &lt;h:dataTable var="row"  border="0" value="#{beanCategory.collection}" binding="#{beanCategory.datatable}"&gt;
  
      &lt;f:facet name="header"&gt;
          &lt;h:outputText value="Categories"/&gt;
      &lt;/f:facet&gt;
      &lt;h:column&gt;
          &lt;f:facet name="header"&gt;
              &lt;h:outputText value="Description"/&gt;
          &lt;/f:facet&gt; 
  
              &lt;h:inputText id="input1" value="#{row.description}" valueChangeListener="#{row.inputChanged}"/&gt;
  
  
          &lt;/h:column&gt;
      &lt;h:column&gt;
          &lt;f:facet name="header"&gt;
              &lt;h:outputText value="Id"/&gt;
          &lt;/f:facet&gt;
          &lt;h:outputText id="id" value="#{row.id}"/&gt;
      &lt;/h:column&gt;
      &lt;h:column&gt;
              &lt;h:commandButton value="Delete" type="submit" action="#{beanCategory.remove}"&gt;
  
                  &lt;f:setPropertyActionListener target="#{beanCategory.selectedcategory}" value="#{row}"/&gt;
              &lt;/h:commandButton&gt;
              &lt;h:commandButton value="Save" action="#{beanCategory.update}"
                  &gt;
                  &lt;f:setPropertyActionListener
                      target="#{beanCategory.selectedcategory}" value="#{row}" /&gt;
  
              &lt;/h:commandButton&gt;
          &lt;/h:column&gt;
  &lt;/h:dataTable&gt;
  
  &lt;h:inputText id="text1"&gt;&lt;/h:inputText&gt;  &lt;h:commandButton action="#{beanCategory.addCategory}" value="Add" type="submit" id="submitbutton"&gt;
  
  &lt;/h:commandButton&gt;
  &lt;br/&gt;&lt;br/&gt;
  Messages    
  
  &lt;h:messages&gt;&lt;/h:messages&gt;&lt;br /&gt;&lt;br /&gt;
  
  &lt;/h:form&gt;   
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Backing Bean is here</p>
  
  <pre><code>package net.bssuk.timesheets.controller;
  
  import java.io.Serializable;
  
  import java.util.List;
  
  import javax.faces.component.UIInput;
  import javax.faces.component.html.HtmlDataTable;
  import javax.faces.context.FacesContext;
  
  import javax.persistence.*;
  
  import net.bssuk.timesheets.model.Category;
  
  @javax.inject.Named("beanCategory")
  @javax.enterprise.context.SessionScoped
  
  public class BeanCategory implements Serializable {
  
  private List&lt;Category&gt; collection;
  private EntityManagerFactory emf;
  private EntityManager em;
  private int selectedid;
  private Category selectedcategory;
  private HtmlDataTable datatable;
  
  private static final long serialVersionUID = 1L;
  
  public BeanCategory() {
      // TODO Auto-generated constructor stub
  
      System.out.println("Bean Constructor");
  
  }
  
  public String addCategory() {
      try {
          this.emf = Persistence.createEntityManagerFactory("timesheets1");
          System.out.println("Changed - Now attempting to add");
          System.out.println("Ready to do cateogory");
          Category category = new Category();
  
          FacesContext context = FacesContext.getCurrentInstance();
          UIInput input = (UIInput) context.getViewRoot().findComponent(
                  "mainform:text1");
  
          String value = input.getValue().toString();
          if (value != null) {
              category.setDescription(input.getValue().toString());
          } else {
              category.setDescription("Was null");
          }
          this.em = this.emf.createEntityManager();
          EntityTransaction tx = em.getTransaction();
          tx.begin();
          em.persist(category);
          tx.commit();
          em.close();
          emf.close();
          // return "index.xhtml";
      } catch (Exception e) {
          e.printStackTrace();
      }
      return "return.html";
  }
  
  public String remove() {
      try {
          this.emf = Persistence.createEntityManagerFactory("timesheets1");
          System.out.println("Getting Collection");
          this.em = this.emf.createEntityManager();
  
          FacesContext context = FacesContext.getCurrentInstance();
  
          System.out.println("Number found is " + this.selectedid);
  
          if (selectedcategory != null) {
  
              System.out.println("removing "+selectedcategory.getId()+" - " +selectedcategory.getDescription());
              EntityTransaction tx = em.getTransaction();
              tx.begin();
              System.out.println("Merging..");
              this.em.merge(selectedcategory);
              System.out.println("removing...");
              this.em.remove(selectedcategory);
              tx.commit();
              em.close();
              emf.close();
          }else{
              System.out.println("Not found");
          }
          return "index.xhtml";
      } catch (Exception e) {
          e.printStackTrace();
          return "index.xhtml";
      }
  
  }
  
  public String update() {
      try {
          this.emf = Persistence.createEntityManagerFactory("timesheets1");
          System.out.println("Update Getting Collection");
          Category category = (Category) getDatatable().getRowData();
  
          FacesContext context = FacesContext.getCurrentInstance();
          System.out.println("PHASE ID="+context.getCurrentPhaseId().toString());
  
          if (category != null) {
              // DESCRIPTION VALUE BELOW IS ALWAYS OLD VALUE (IE DATA IN DATABASE)
              System.out.println("updating "+category.getId()+" - " +category.getDescription());
  
              this.em = this.emf.createEntityManager();
              EntityTransaction tx = em.getTransaction();
              tx.begin();
              em.merge(category);
              tx.commit();
              em.close();
              emf.close();
          }else{
              System.out.println("Not found");
          }
          return "index.xhtml";
      } catch (Exception e) {
          e.printStackTrace();
          return "";
      }
  }
  
  public void setCollection(List&lt;Category&gt; collection) {
      this.collection = collection;
  }
  
  public List&lt;Category&gt; getCollection() {
      // this.emf=Persistence.createEntityManagerFactory("timesheets1");
      // System.out.println("Getting Collection");
      try {
          this.emf = Persistence.createEntityManagerFactory("timesheets1");
          this.em = this.emf.createEntityManager();
          Query query = this.em.createNamedQuery("findAll");
          this.collection = query.getResultList();
          return this.collection;
      } catch (Exception e) {
          e.printStackTrace();
          return null;
      }
  }
  
  public void setSelectedid(int id) {
      this.selectedid=id;
  }
  
  public void setSelectedcategory(Category selectedcategory) {
      this.selectedcategory = selectedcategory;
  }
  
  public HtmlDataTable getDatatable() {
      return datatable;
  }
  
  public void setDatatable(HtmlDataTable datatable) {
      this.datatable = datatable;
  }
  public Category getSelectedcategory() {
  return selectedcategory;
   }
  
  
  
  }
  </code></pre>
  
  <p>My Mapped entity for JPA is here</p>
  
  <pre><code>package net.bssuk.timesheets.model;
  import java.io.Serializable;
  import javax.persistence.*;
  
  
  /**
   * The persistent class for the CATEGORIES database table.
   * 
  */
  @Entity
  @Table(name="CATEGORIES")
  @NamedQuery(name="findAll", query = "SELECT c from Category c")
  public class Category implements Serializable {
  private static final long serialVersionUID = 1L;
  
  private String description;
  
  @Id 
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  
  public Category() {
  }
  
  public String getDescription() {
      return this.description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  public int getId() {
      return this.id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  }
  </code></pre>
  
  <p>OK - Updated my code to follow example. I have tried to incorporate an EJB into the scenario as follows</p>
  
  <pre><code>package net.bssuk.timesheets.ejb;
  
  import java.util.List;
  
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  import net.bssuk.timesheets.model.Category;
  @Stateless
  public class CategoryEJB implements CategoryEJBRemote {
  
  @PersistenceContext(unitName="timesheets1")
  private EntityManager em;
  
  @Override
  public List&lt;Category&gt; findCategories() {
      // TODO Auto-generated method stub
      System.out.println("find categories");
      Query query = em.createNamedQuery("findAll");
      return query.getResultList();
  }
  
  @Override
  public Category createCategory(Category category) {
      // TODO Auto-generated method stub
      em.persist(category);
      return category;
  }
  
  @Override
  public Category udpateCategory(Category category) {
      // TODO Auto-generated method stub
      return em.merge(category);
  }
  
  @Override
  public void deleteCategory(Category category) {
      // TODO Auto-generated method stub
          em.remove(em.merge(category));
  }
  
  }
  </code></pre>
  
  <p>My EJB is below</p>
  
  <pre><code>package net.bssuk.timesheets.ejb;
  
  import java.util.List;
  
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  import net.bssuk.timesheets.model.Category;
  @Stateless
  public class CategoryEJB implements CategoryEJBRemote {
  
  @PersistenceContext(unitName="timesheets1")
  private EntityManager em;
  
  @Override
  public List&lt;Category&gt; findCategories() {
      // TODO Auto-generated method stub
      System.out.println("find categories");
      Query query = em.createNamedQuery("findAll");
      return query.getResultList();
  }
  
  @Override
  public Category createCategory(Category category) {
      // TODO Auto-generated method stub
      em.persist(category);
      return category;
  }
  
  @Override
  public Category udpateCategory(Category category) {
      // TODO Auto-generated method stub
      return em.merge(category);
  }
  
  @Override
  public void deleteCategory(Category category) {
      // TODO Auto-generated method stub
          em.remove(em.merge(category));
  }
  
  }
  </code></pre>
  
  <p>Can anyone suggest if this sort of looks ok? Or have I completely lost the plot with it!</p>