0 Replies Latest reply on Sep 16, 2009 10:17 PM by Tanya Ruttenberg

    Use EntityHome to reference an object not by the id

    Tanya Ruttenberg Expert

      Seam 2.1.2. Project initially created with seam-gen.


      I have an Office object with an id officeId.  I can look at Office number 5246 in the browser using a URL like this:




      http://my.web.site/assetTag/Office.seam?officeId=5246





      I understand that the officeId gets passed via Office.page.xml parameter officeId:




      <param name="officeId" value="#{OfficeHome.officeOfficeId}" />
      




      As far as I can figure out, the magic of EntityHome is that it knows exactly what to do with a parameter that is the id of the object.  The object instance gets created or?? What exactly does the wired() method do?


      It seems that the instance id gets set via the setOfficeOfficeId method.  Though I traced this via the debugger, I haven't quite figured out the interaction between the wired() and setOfficeOfficeId() methods.  Wired() seems to happen first?  I don't get it.  Probably owing to my lack of understanding of wired.  Can someone explain the EntityHome methods?  The docs I found are kind of skimpy.


      Now--I have another attribute, siteCode. I'd like to be able to view an Office by passing in a sitecode parameter like this




      http://my.web.site/assetTag/Office.seam?siteCode=457







      <param name="siteCode" value="#{officeHome.officeSiteCode}" />





      I want setOfficeSiteCode method to access the instance id via a database query like this (I use native sql because I need to be able to match on a regular expression -- need that word boundary!):




      public void setSiteCode(String siteCode) {
        String query = ("SELECT office_id FROM office_view " +
          "WHERE site_code REGEXP '[[:<:]]" + siteCode + "[[:>:]]'");
        Integer id2 = (Integer) entityManager.createNativeQuery(query)
          .getSingleResult();
        setOfficeOfficeId(id2);
        officeSiteCode = siteCode;
      }





      I figured that when I passed in the sitecode parameter that setSiteCode would set the instance id and then wired() would work as it does when I actually do pass in the id.


      This is not what happens however.  For some reason, even though I am passing the siteCode into the class, setOfficeSiteCode never gets called and an empty object is created.  My view is rendered with empty fields where the data should be.


      Tanya