2 Replies Latest reply on Aug 22, 2007 7:18 AM by Sebastian Stenerdal

    Finding an entity using a non-@Id unique identifier

    Sebastian Stenerdal Newbie

      Hi!

      I'm using an EntityHome-object which I'm using to view Employees on a viewEmployee.xhtml page, using in pages.xml with no problems.

      My problem now is that I want to use viewEmployee.xhtml to view an Employee based on a unique employement-number instead, and that's where I run into problems.

      This is my current attempt, where I first find the corresponding id, and then try to load viewEmployee.xhtml the usual way:

      (foo.xhtml)

      <h:form>
       <h:inputText id="employmentNumber" value="#{employmentNumber}"/>
       <h:commandButton id="find" value="Find" action="#{employeeDao.findByEmploymentNumber}"/>
      </h:form>
      


      (components.xml)
      <fwk:entity-query name="employeeIdByEmploymentNumber"
       entity-manager="#{entityManager}"
       ejbql="select e.id from Employee e">
       <fwk:restrictions>
       <value>employmentNumber = #{employmentNumber}</value>
       </fwk:restrictions>
      </fwk:entity-query>
      


      (EmployeeDao.java (Extends EntityHome))
      public String findByEmploymentNumber() {
       EntityQuery eQuery = (EntityQuery)Component.getInstance("employeeIdByEmploymentNumber");
       Long l = null;
       if(eQuery.getResultCount() > 0)
       l = (Long)eQuery.getSingleResult();
       if(l != null) {
       setId(l);
       return "found";
       } else {
       facesMessages.add("Not found!");
       }
       return "";
      }
      


      (pages.xml)
      <page view-id="/foo.xhtml">
       <navigation from-action="#{employeeDao.findByEmploymentNumber}">
       <rule if-outcome="found">
       <redirect view-id="/viewEmployee.xhtml">
       <param name="employeeId" value="#{employeeDao.id}" converterId="javax.faces.Long"/>
       </redirect>
       </rule>
       </navigation>
      </page>
      


      The id is a Long, and the employmentNumber is a String.

      This all works, except that I get LazyInitializationExceptions in viewEmployee.xhtml when trying to render connected entities. If I just reload the "An Error Occurred:"-page, though, everything works perfectly. So somehow, my solution closes the session that I'm keeping the Employee in.

      I'm not sure if this is a good solution for this at all, so any help or suggestions would be greatly appreciated. :)
      /uke

        • 1. Re: Finding an entity using a non-@Id unique identifier
          Pete Muir Master

          You could override the find() method on EntityHome in your employeeDao

          • 2. Re: Finding an entity using a non-@Id unique identifier
            Sebastian Stenerdal Newbie

            Thanks for your response!

            I tried overriding the find()-method with the following:

            @Override
            public Employee find() {
             EntityQuery eQuery = (EntityQuery)Component.getInstance("employeeIdByEmploymentNumber");
             Long l = null;
             if(eQuery.getResultCount() > 0)
             l = (Long)eQuery.getSingleResult();
            
             getEntityManager().joinTransaction();
             Employee result = getEntityManager().find( getEntityClass(), (l == null ? getId() : l));
             if (result==null) result = handleNotFound();
             return result;
            }
            


            I then aimed the commandButton towards /viewEmployee.xhtml, and added to the -tag for it, but it gives me LazyInitializationException and TransientObjectException.

            I wasn't really sure on what to do with the overridden find method, since I'm not really sure why my session is closed, and why it works when I reload the page.

            Thanks
            uke