4 Replies Latest reply on Jan 26, 2010 1:52 PM by Max Rydahl Andersen

    seam generate entities for many-to-many relationships

    Wouter Hartog Newbie

      I have a MySQL database with 3 tables:

      -User

      -Role

      -UserRoles

      The UserRoles table is just there to enable the many-to-many relationship between User and Role.

       

      When I run generate entities, it creates three different entity classes, while I only expect two:

      - one for the User with a @ManyToMany annotation pointing to the Role table with joinColum = UserRole

      - one for the Role (with or without a ManyToMany annotation pointing to the UserRole table)

      Instead it also creates an entity class for the UserRoles table, which I don't need / want, and the relation to the UserRole table is a OneToMany relation.

       

      Is there any way you can influence / setup the behavior of 'generate entities' to look at the many-to-many relationships, or is this always something you have to change manually in the entity classes later, after 'generate entities' did its work?

       

      Versions:

      - MySQL 5.1

      - Seam 2.2

      - Jboss Tools 3.0

      - Eclipse 3.4.2

        • 1. Re: seam generate entities for many-to-many relationships
          Wouter Hartog Newbie

          Found the solution:

          I had to define my UserRole table different.

          I had one UserRoleID column as the primary key, and the userId and roleId as just FK to the other tables, like this:

          userRoleId (PK)

          roleId (FK)

          userId (FK)

           

          When I changed the table structure to this (removed the original PK column):

          roleID (PK, FK)

          userId (PK, FK)

           

          seam generate entities worked as I wanted to: on the Entity class User, I had a @ManyToMany annotation, linking to the Role table, using the UserRole table as the joinTable.

           

          a great tool!

          • 2. Re: seam generate entities for many-to-many relationships
            Max Rydahl Andersen Master

            Yes, your solution is correct since otherwise we just see the tables are three related entities. No way to detect the many-to-many.

             

            But to answer your question about convincing/controlling seam-gen (or rather hibernate tools) beyond what is in your database lookup rev-eng.xml in the documentation or google

            • 3. Re: seam generate entities for many-to-many relationships
              Benjamin Janssens Newbie

              Hello,

              yes the seam gen generates only two entity, but in the xhtml (supplier.xhtml in my case) view file, I have this :

               

              <h:outputText value="There are no lkSupplierAddresses associated with this supplier."
                                 rendered="#{empty supplierHome.lkSupplierAddresses}"/>

               

              <rich:dataTable value="#{supplierHome.lkSupplierAddresses}"
                                     var="_lkSupplierAddress"
                                rendered="#{not empty supplierHome.lkSupplierAddresses}"
                              rowClasses="rvgRowOne,rvgRowTwo"
                                      id="lkSupplierAddressesTable">

               

              lkSupplierAddresses is my table with the two IDs as a primary key (suppId and addId), when I try to access this outputText, or dataTable I have this exception :

               

              16:29:15,885 ERROR [viewhandler] Error Rendering View[/Supplier.xhtml]
              javax.faces.FacesException: javax.el.PropertyNotFoundException: /Supplier.xhtml @66,77 rendered="#{empty supplierHome.lkSupplierAddresses}": Property 'lkSupplierAddresses' not found on type com.orderform.session.SupplierHome_$$_javassist_seam_11

               

              So  there is a problem in the generation of the xhtml files.

               

              Do you know is there is a fix for the generation files ftl ?

               

              Thanks, Benjamin

              • 4. Re: seam generate entities for many-to-many relationships
                Max Rydahl Andersen Master
                That is better asked at the seam forums since we are simply using the seam-gen templates here - but I don't believe seam-gen ever supported many-to-many generation fullly.