6 Replies Latest reply on Aug 18, 2008 11:55 PM by gsimone

    generate-ui: generated entity home class does not compile

    gsimone

      Hello. I am having a problem with the seam generate-ui code. It generates entity home source code that will not compile, in the following situations:



      1. an entity has a ManyToOne relationship with an entity in another package

      2. an entity has two or more ManyToOne relationships with entities of the same class.



      A proposed fix is at the end of this note.


      Here is a sample for issue #1:


      package com.domain.model.person;
      
      import com.domain.model.location.Address;
      
      @Entity
      public class PersonAddress
      {
         private Address address;
         private PersonAddressTypeLookup addressTypeLookup;
         private Person person;
      
         @ManyToOne @NotNull
         public Address getAddress() {return address;}
      
         @ManyToOne @NotNull
         public PersonAddressTypeLookup getAddressTypeLookup()   
         { return addressTypeLookup; }
      
         @ManyToOne @NotNull
         public Person getPerson() {return person;}
         // setters omitted
      }
      


      which generates:


      package com.domain.action;
      
      import com.domain.model.person.*;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityHome;
      
      @Name("personAddressHome")
      public class PersonAddressHome extends EntityHome<PersonAddress>
      {
         // code omitted...
      
         public void wire()
         {
            getInstance();
            Address address = addressHome.getDefinedInstance();
            if (address != null)
            {
               getInstance().setAddress(address);
            }
            PersonAddressTypeLookup addressTypeLookup = addressTypeLookupHome
                  .getDefinedInstance();
            if (addressTypeLookup != null)
            {
               getInstance().setAddressTypeLookup(addressTypeLookup);
            }
            Person person = personHome.getDefinedInstance();
            if (person != null)
            {
               getInstance().setPerson(person);
            }
         }
      



      The generated code imports all entities in com.domain.model.person, but does not import anything from com.domain.model.location so the compilation fails in the second line of the wire() method (Address cannot be resolved to a type).


      Here is a sample for issue #2.



      package com.domain.model.person;
      
      import javax.persistence.Entity;
      import javax.persistence.ManyToOne;
      
      import org.hibernate.validator.NotNull;
      
      @Entity
      public class PersonRelation
      {
         private PersonRelationTypeLookup personRelationTypeLookup;
         private Person primaryPerson;
         private Person relatedPerson;
      
         @ManyToOne @NotNull
         public PersonRelationTypeLookup getPersonRelationTypeLookup()
         {
            return personRelationTypeLookup;
         }
      
         @ManyToOne @NotNull
         public Person getPrimaryPerson()
         {
            return primaryPerson;
         }
      
         @ManyToOne @NotNull
         public Person getRelatedPerson()
         {
            return relatedPerson;
         }
      
         // setters omitted
      }
      



      which generates


      @Name("personRelationHome")
      public class PersonRelationHome extends EntityHome<PersonRelation> {
      
           @In(create = true)
           PersonRelationTypeLookupHome personRelationTypeLookupHome;
           @In(create = true)
           PersonHome personHome;
           @In(create = true)
           PersonHome personHome;
            // ...
      
      



      In this case we get a compile error because both PersonHome variables have the same name.


      To fix, modify ./seam-gen/src/EntityHome.java.ftl as follows:


      around line 79, replace:


      ${parentPojo.shortName} ${property.name}= ...
      



      with


      ${parentPojo.packageName}.${parentPojo.shortName} ${property.name}=...



      which results in a fully qualified com.domain.model.location.Address instead of just Address.


      then replace lines 13 and 76:


      <#assign parentHomeName = util.lower(parentPojo.shortName) + "Home">
      


      with


      <#assign parentHomeName = property.name + "Home">



      the variables are now primaryPersonHome and relatedPersonHome instead of both being personHome.


      And it seems to fix my compile problems.


      However ... I am curious to know if anyone else has run into either of these problems.  Or if there is a better solution.