3 Replies Latest reply on Sep 21, 2008 12:38 PM by atao

    seam-gen and multiple foreign keys (again)

    adrianflynn

      Hi All


      I'm sorry for resurrecting this old bug-bear but I am trying to build a CRUD front-end application for my existing EJB3-based system. (over 200 entities)


      Many of my entities contain multiple references to the same entity type - for example, a flight has a departure airport and a destination airport so we have two distinct references to the airports entity / table.


      I have been researching this problem and found JIRA JBSEAM-994 which attempts but apparently fails to resolve the problem by means of code generation.


      I am perfectly happy to put in the work to manually 'massage' the 'seam-gen'ed code, but I don't know where to start, and what to change.


      Gavin King suggests back on May 07 that it is possible, just difficult to do automatically:



      No, this patch is not a correct solution, in fact I don't see any easy way to arrive at a correct solution - this is a super-difficult one to solve by code generation, it needs human-written code.


      My question however, is 'How?'


      A single example would be sufficient to get me going :)


      Any guidance would be gratefully appreciated


      Thanks


      Adrian

        • 1. Re: seam-gen and multiple foreign keys (again)
          jayantada
          In case you are getting compile errors because of duplicate defintions, I used the following to modify the template EntityHome.java.ftl in seamgen directory.
          public class ${entityName}Home extends ${pojo.importType("org.jboss.seam.framework.EntityHome")}<${entityName}>
          {
          <#assign prevEntry = " ">
          <#foreach property in pojo.allPropertiesIterator>
          <#if c2h.isManyToOne(property)>
          <#assign parentPojo = c2j.getPOJOClass(cfg.getClassMapping(property.value.referencedEntityName))>
          <#assign parentHomeName = util.lower(parentPojo.shortName) + "Home">
          <#if parentHomeName !=prevEntry>
           
              @${pojo.importType("org.jboss.seam.annotations.In")}(create=true)
              ${parentPojo.shortName}Home ${parentHomeName};
          <#assign prevEntry = parentHomeName>
          </#if>
          </#if>
          </#foreach>

          A quick & dirty solution but got tired of fixing manually

          • 2. Re: seam-gen and multiple foreign keys (again)
            adrianflynn

            Thank you for the suggestion, Jayanta, and your code does indeed prevent the compilation errors in the FlightHome class, however the resulting application permits CRUD operations to be performed on the departureAirport field of a Flight, but there is no means to access the destinationAirport.


            I believe that the original structure showing both fields in the Home class annoted with @In, but with different names is a more correct starting point:


                 @In(create = true)
                 AirportHome departureAirportHome;
                 @In(create = true)
                 AirportHome destinationAirportHome;
            



            The wire() method also needs manual modification to make this work.


            However, I am lost as to what changes are required in the .pages.xml and .xhtml files.


            I suspect that Flight.page.xml will need the following two lines duplicated and renamed:


               <param name="airportFrom"/>
               <param name="airportId" value="#{airportHome.airportId}"/>
            


            The Flight.xhtml page seems ok but I can't see any way to avoid duplicating all eight
            Airport files in my source tree:



            • Airport.page.xml

            • Airport.xhtml

            • AirportEdit.page.xml

            • AirportEdit.xhtml

            • AirportList.page.xml

            • AirportList.xhtml

            • AirportHome.java

            • AirportList.java



            ... with the only difference being the @Name Annotations in the java files, and renaming the links in the destinationAirport tab of the Flight.xhtml to use the new component name eg Airport2


            While class re-use is common in 'ordinary' Java, I cannot find any examples of it being used in Seam.


            Can anyone advise me please?


            Thanks


            Adrian


            • 3. Re: seam-gen and multiple foreign keys (again)
              atao

              Adrian,


              The patch proposed (doublon.zip and doublon2.patch) for JBSEAM-994 works. And it does the job automatically.


              If you want to do it manually, you can still check how the patch does the job. Yes, there are quite a lot of code to add. Mainly to deal with one seam component per attribute of same type.