[jboss-user] [JBoss Seam] - Finding an entity using a non- at Id unique identifier

uke do-not-reply at jboss.com
Wed Aug 22 05:28:46 EDT 2007


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:


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


  | <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 "";
  | }


  | <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. :)

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4076693#4076693

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4076693

More information about the jboss-user mailing list