3 Replies Latest reply on Oct 11, 2007 12:41 AM by Terry

    EntityHome and Primay Key value assignment for new record

    Terry Apprentice

      My JSF application require primary keys to be custom generated when record is inserted. and I thought EntityHome.persist() was the place to put the code which generate the primary key for new records. so I had as below. but I get huge list of errors.

      1. can anyone please suggest the best way to do this?
      2. is it also possible to use my keyGenerator seam component in JSF to assign value to the EntityHome.instance.id field?

      
      @Name("securityRoleHome")
      public class SecurityRoleHome extends EntityHome<SecurityRole> {
      ...
      
      @Override
      public String persist() {
       if (Strings.isNull(getSecurityRoleId()) && !this.isManaged()) {
       KeyGenerator keyGen = (KeyGenerator) Component.getInstance (KeyGenerator.class, true);
       setSecurityRoleId(keyGen.getKey(Constant.Database.KeySuffix.ADMIN));
       }
       return super.persist();
      }
      ...
      }
      




        • 1. Re: EntityHome and Primay Key value assignment for new recor
          Terry Apprentice

          I found my mistake, I wasn't using the right instance.

          the following fixed the problem 1.

          @Override
          public String persist() {
          if (Strings.isNull(getInstance().getId()) && !this.isManaged()) {
          KeyGenerator keyGen = (KeyGenerator) Component.getInstance (KeyGenerator.class, true);
          getInstance().setId(keyGen.getKey(Constant.Database.KeySuffix.ADMIN));
          }
          return super.persist();
          }


          Any clue to my question 2 will be appreciated.

          • 2. Re: EntityHome and Primay Key value assignment for new recor
            Stuart Douglas Master

            I am to 100% sure what you mean, by q2 but I think you want to look up the section in the hibernate entityManager docs on event listeners. I think you could set a PrePersist listener on you keygen component, and this would acomplish what you are after, I am not sure though.

            • 3. Re: EntityHome and Primay Key value assignment for new recor
              Terry Apprentice

              I tried @PrePersist but got struck with popular hibernate bug... Id must be be assigned manually..., it's to do with hibernate doing validations before invoking PrePersist I think...

              The bug is fixed in latest hibernate GA release, but I am not sure what else will break if i use it with Redhat Developer Beta 1.

              So as a workaround, Im keeping my code in MyEntityHome for now, as above.

              Q2. I was trying to use my generated Id as a default value for the jsf h:inputHidden tag. The tage is bound to the ID property of the entity bean. So I thought that way it will work automatically, as JSF will assign value in hidden bound control to the backing entity bean.

              I just couldn't figure out how to have h:inputHidden bound to the entity property and at the same time make it use my generate Id as a default value.

              h:inputHidden id="code" required="true" size="50" maxlength="50" value="#{securityRoleHome.instance.id}"
              /h:inputHidden