Menu

One-to-many Support

Help
rjemmons
2007-12-08
2013-04-25
  • rjemmons

    rjemmons - 2007-12-08

    Hibernate seems to support one-to-many relationships by creating HashSets in the bean for the "one" table.  For example, if you have PRODUCT_TYPE and PRODUCT tables, Hibernate creates a HashSet in the PRODUCT_TYPE bean to hold a set of PRODUCT records for each PRODUCT_TYPE record.

    I do not see this with Sql2java, which seems to create two independent beans.  When you retrieve a PRODUCT_TYPE record into its bean, it does not include its set of related products.

    Am I missing something?

     
    • Alain Fagot Béarez

      Sorry for the delay... It looks like I bypassed this post back then in December.  Thanks to Horst for alerting about this in his post!

      You are right that the generated code for the bean does not have any reference to the other referenced beans.  However sql2java does generate the methods to load them.  They are in the manager of the bean.

      Following your example, you will find in the ProductTypeManager a method "Product[] getProducts(ProductType bean)" to retrieve from the database all the Product beans related to your ProductType bean.

      You also have a method "setProduct(ProductType bean, Product related)" to establish the relation between your current ProductType and a new related Product.

      Unlike Hibernate which enables both lazy and eager loading, sql2java generated code only permits lazy loading.  Would you like to make it eager, you would need to modify the bean.java.vm template (and contribute it back as a patch;-).

      Hope it is now clearer.

      Alain

       
    • rjemmons

      rjemmons - 2008-04-09

      Thanks.  That makes it clearer.  I have to review my code.  I think I was coding SQL to accomplish this.  The "Product[] getProducts(ProductType bean)" methods should be simpler.

       
      • Alain Fagot Béarez

        If you'd like to get some simpler code generated, please post an example of the resulting code you are expecting.

        After all, many of the templates were contributed by users as you.

         
        • rjemmons

          rjemmons - 2008-04-10

          Sorry, I did not mean that your genereated code should be simpler.  I meant that my code would probably be simpler if I used your code properly, rather than writing my own SQL.

           
    • Aneesh Vijendran

      Brilliant! I was about to move from sql2java.
      I have also modified a template to generate struts2 actions with CRUD functionality. Where should I put it, once I test them in my current project?

      Regards
      Aneesh

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.