0 Replies Latest reply on Apr 3, 2008 6:18 PM by Gerhard Schaden

    DataModelSelection and transient object

    Gerhard Schaden Newbie

      I'm using seam 2.0.1.GA and tomcat 6.0.16.


      There is one action in session scope



      import java.util.List;
      
      import javax.ejb.Stateful;
      import javax.persistence.EntityManager;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.datamodel.DataModelSelection;
      import org.jboss.seam.annotations.web.RequestParameter;
      import org.jboss.seam.faces.FacesMessages;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.security.Identity;
      
      
      @Name("deviceaction")
      @Scope(ScopeType.SESSION)
      public class DeviceAction {
      
              @Logger
              Log log;
      
              @In
              private FacesMessages facesMessages;
      
              @In
              private EntityManager entityManager;
      
              @DataModel
              private List<Device> devices;
      
              @DataModelSelection(value="devices")
              private Device device;
      
          private String searchString;  
          private int pageSize = 10;
          private int page;
      
      
              public void persist() {
                      log.debug("persisting device instance");
                      try {
                              entityManager.persist(device);
                              log.debug("persist successful");
                      } catch (RuntimeException re) {
                              log.error("persist failed", re);
                              throw re;
                      }
              }
      
      
              public void deleteDevice() {
                      log.info("delete Object #0", device);
                      if (device != null) {
                              devices.remove(device);
                              entityManager.remove(device);
                              entityManager.flush();
                              facesMessages.add("Device " + device.getSnr() + " removed");
                      }
              }
      
      
              public Device findById(int id) {
                      log.debug("getting Configobject instance with id: " + id);
                      try {
                              Device instance = entityManager.find(Device.class, id);
                              log.debug("get successful");
                              return instance;
                      } catch (RuntimeException re) {
                              log.error("get failed", re);
                              throw re;
                      }
              }
      
              @SuppressWarnings("unchecked")
              public void getAll() {
                      log.debug("getting all Configobjects");
                      try {
                              devices = entityManager.createQuery("select e from Device e").getResultList();
      
                      } catch (RuntimeException re) {
                              log.error("get failed", re);
                              throw re;
                      }
              }
      
              public void logDevice(){
                      log.info("selected device #0", device);
              }
      
          public void setPageSize(int pageSize) {
              this.pageSize = pageSize;
          }
      
          public int getPage() {
              return page;
          }
      
          public void setPage(int page) {
              this.page = page;
          }
      
          public boolean isNextPageAvailable() {
              return (devices != null) && (devices.size() == pageSize);
          }
      
          public String getSearchString() {
              return searchString;
          }
      
          public void setSearchString(String searchString) {
              this.searchString = searchString;
          }
      
          public void nextPage() {
              page++;
              queryEntries();
          }
      
          public void find() {
              page = 0;
              if (getSearchPattern().length()<3)
                      return;
              if (devices != null) {
                  devices.clear();
              }
              queryEntries();
          }
          
          @SuppressWarnings("unchecked")
          private void queryEntries() {
              log.info("Querying devices ... #0", getSearchPattern());
              devices = entityManager.createQuery(
                      "select e from Device e where lower(e.snr) like #{pattern}" +
                              " or lower(e.mac) like #{pattern}" 
      //                 + " and (e.user.name = #{user.name})
                              )
                      .setMaxResults(pageSize)
                      .setFirstResult(page * pageSize).getResultList();
      
          }
      
          @Factory(value = "pattern", scope = ScopeType.EVENT)
          public String getSearchPattern() {
              return (searchString == null) ? "%"
                      : ('%' + searchString.toLowerCase().replace('*', '%') + '%');
          }
      
              public int getPageSize() {
                      return pageSize;
              }
      
              
      }
      



      if i click on a link


                              <s:link onclick="return confirmDeletion()" id="deleteEntry" action="#{deviceaction.deleteDevice}">
                                  <h:graphicImage value="/img/delete.png" alt="[Delete]" style="border:0"/>
                              </s:link
      



      I get the following


      INFO: delete Object Device: 1 dfe asdf
      03.04.2008 16:47:27 org.hibernate.event.def.DefaultDeleteEventListener deleteTransientEntity
      INFO: handling transient entity in delete processing
      



      why is this object transient? I tried to merge it, but that didnt work either. In this case hibernate tried to insert the object and got an error, because of duplicate key.