1 Reply Latest reply on Nov 2, 2011 1:28 PM by nathandennis

    Seam Persistence Help!

    fygiof
      Hi, i am a beginner of seam. I make a page that list records, create a new record and update selected record. Creating and listing records works fine but updating doesn't.

      Please help me what is the wrong with this code given below

      --------------------------------------------------------------------------
      package com.entity;

      import java.io.Serializable;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Table;

      import org.hibernate.validator.Length;

      @Entity
      @Table(name = "AUTHOR", schema = "PUBLIC")
      //@Where(clause = "Deleted=0")
      public class Author implements Serializable {

           /**
            *
            */
           private static final long serialVersionUID = 1L;
           @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
           @Column(name = "id", unique = true, nullable = false)
           private Long id;
           @Length(max = 20)
           private String name;
           @Length(max = 20)
           private String surname;

           public Author(){}
           
           public Author(Long id) {
                super();
                this.id = id;
           }

           public Long getId() {
                return id;
           }

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

           public String getName() {
                return name;
           }

           public void setName(String name) {
                this.name = name;
           }

           public String getSurname() {
                return surname;
           }

           public void setSurname(String surname) {
                this.surname = surname;
           }
      }
      ---------------------------------------------------------------------------
      package com.home;

      import javax.persistence.EntityManager;

      import oracle.jdbc.driver.DMSFactory;

      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityHome;

      import com.entity.Author;

      @Name("authorHome")
      public class AuthorHome extends EntityHome<Author> {

           private static final long serialVersionUID = 1L;
           @In
           EntityManager entityManager;

           public EntityManager getEntityManager() {
                return entityManager;
                }
           
           @Override
           public Class<Author> getEntityClass() {
                return Author.class;
           }

           public String update() {
                String value = super.update();
                entityManager.flush();
                return value;
           }
           
           @Override
           public void setId(Object id) {
                super.setId(id);          
           }

           public String persist() {
                String value = super.persist();
                entityManager.flush();
                return value;
           }

           public void reload() {
                if (isManaged()) {
                     entityManager.refresh(getInstance());
                }
           }
      }
      -------------------------------------------------------------------------
      package com.list;

      import java.util.List;

      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.datamodel.DataModelSelection;

      import com.entity.Author;
      import com.home.AuthorHome;


      @Name("authorList")
      @Scope(ScopeType.CONVERSATION)
      public class AuthorList extends com.session.AuthorList {

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

           @DataModel
           List<Author> dmAuthor;

           @DataModelSelection
           Author dmsAuthor;

           @In(create = true)
           AuthorHome authorHome;
           
           private boolean allSelected;
           
           public void initHome() {
                authorHome.clearInstance();
                authorHome.setId(null);
           }
           
           @Factory("dmAuthor")
           public void initializeDataModel() {          
                authorHome.reload();
                refresh();
                dmAuthor = getResultList();
           }
           
           public void assignSelected() {
                authorHome.setId(dmsAuthor.getId());
           }
           
           public void deleteSelected() {
                for (Author author : dmAuthor)
                     //if (author.isSelected()) {
                          authorHome.setInstance(author);
                          authorHome.remove();

                     //}
                initializeDataModel();
                allSelected = false;
           }
           

           public boolean isAllSelected() {
                return allSelected;

           }
           
           public void setAllSelected(boolean allSelected) {
                this.allSelected = allSelected;

           }
           
           public void checkSelected() {
                //for (Author author : dmAuthor)
                     //author.setSelected(allSelected);
           }
      }
      ----------------------------------------------------------------------
      package com.session;

      import java.util.Arrays;
      import org.jboss.seam.framework.EntityQuery;

      import com.entity.Author;

      public class AuthorList extends EntityQuery<Author>
      {

           /**
            *
            */
           private static final long serialVersionUID = 1L;
           private static final String EJBQL = "select author from Author author";
           private static final String[] RESTRICTIONS = {};
           
           private Author author = new Author();
           
           public AuthorList()
          {
              setEjbql(EJBQL);
              setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
          }
           
           public Author getAuthor() {
                return author;
           }
      }
      ----------------------------------------------------------------
      <f:view xmlns="http://www.w3.org/1999/xhtml"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:richfaces="http://richfaces.ajax4jsf.org/rich"
           xmlns:ajax="https://ajax4jsf.dev.java.net/ajax"
           xmlns:a4j="http://richfaces.org/a4j"
           xmlns:rich="http://richfaces.org/rich"
           xmlns:s="http://jboss.com/products/seam/taglib"
           xmlns:p="http://primefaces.prime.com.tr/ui">

           <h:form>
                <rich:panel styleClass="formPanel">
                     <f:facet name="header">Yazar Listesi</f:facet>
                     <rich:extendedDataTable value="#{dmAuthor}" height="400px"
                          width="100%" rows="15" id="tblAuthor" var="_author">
                          <!--
                          <rich:column width="50">
                               <f:facet name="header">
                                    <h:selectBooleanCheckbox value="#{authorList.allSelected}">
                                         <a4j:support event="onchange"
                                              action="#{authorList.checkSelected}" reRender="chkSelected">
                                              <s:conversationId />
                                         </a4j:support>
                                    </h:selectBooleanCheckbox>
                               </f:facet>
                               <h:selectBooleanCheckbox value="#{_author.selected}"
                                    id="chkSelected" />
                          </rich:column>
                           -->
                          <rich:column label="Ad" width="100">
                               <f:facet name="header">Ad</f:facet>
                               <h:outputText value="#{_author.name}" />
                          </rich:column>
                          <rich:column label="Soyad" width="150">
                               <f:facet name="header">Soyad</f:facet>
                               <h:outputText value="#{_author.surname}" />
                          </rich:column>
                          <rich:column>
                               <a4j:commandButton image="/images/i-edit.png"
                                    reRender="panelAuthor" oncomplete="applyMasks();"
                                    action="#{authorList.assignSelected()}">
                               </a4j:commandButton>
                          </rich:column>

                     </rich:extendedDataTable>
                     <a4j:commandButton value="Sil" image="/images/b-delete.png"
                          onclick="if(!confirm('Seçilenler silinecektir. Bu işlemi yapmak istediğinize emin misiniz?'))return false;"
                          action="#{authorList.deleteSelected}" reRender="tblAuthor,growlForm">
                          <s:conversationId />
                     </a4j:commandButton>
                </rich:panel>
                <rich:panel id="panelAuthor" styleClass="formPanel">
                     <h:panelGrid columns="2" columnClasses="formLabel, formInput">

                          <h:outputLabel value="Ad" for="txtName" />
                          <h:inputText value="#{authorHome.instance.name}" styleClass="input"
                               id="txtName" />

                          <h:outputLabel value="Soyad" for="txtSurname" />
                          <h:inputText value="#{authorHome.instance.surname}"
                               styleClass="input" id="txtSurname" />

                          <a4j:commandButton image="/images/b-save.png"
                               action="#{authorHome.persist}">
                               <a4j:support event="oncomplete" action="#{authorList.initHome()}"
                                    reRender="tblAuthor,txtName,txtSurname" />
                          </a4j:commandButton>
                          <a4j:commandButton image="/images/b-update.png"
                               action="#{authorHome.update}">
                               <a4j:support event="oncomplete" action="#{authorList.initHome()}"
                                    reRender="tblAuthor,txtName,txtSurname" />                    
                          </a4j:commandButton>

                     </h:panelGrid>
                </rich:panel>
           </h:form>
      </f:view>