6 Replies Latest reply on Aug 17, 2008 8:11 PM by Kamil Demecki

    lazily initialize collections in their getters

    mars1412 Apprentice

      I am thinking of using lazy initialization of collections in thier getters:
      example:

      private java.util.Set<MyEntity> myEntities;
      
       public java.util.Set<MyEntity> getMyEntities() {
       if (myEntities == null) {
       myEntities = new HashSet<MyEntity>();
       }
       return myEntities;
      
       }



      this would be nice because I would not need the explicit null checks in my buissneslogic.

      however I have a bad feeling about this - I'm if this will work in all cases with lazy loading, hibernate proxies and stuff I might not even know about.

      What do you think?

        • 1. Re: lazily initialize collections in their getters
          jaikiran pai Master

          I might be missing something here, but isn't the following approach simpler?


          private java.util.Set<MyEntity> myEntities = new HashSet<MyEntity>();
          
           public java.util.Set<MyEntity> getMyEntities() {
           return myEntities;
          
           }


          • 2. Re: lazily initialize collections in their getters
            mars1412 Apprentice

            basically it's the same, but with your approach the getter could return null, if anyone called

            setMyEntities(null)


            • 3. Re: lazily initialize collections in their getters
              jaikiran pai Master

               

              "mars1412" wrote:
              basically it's the same, but with your approach the getter could return null, if anyone called
              setMyEntities(null)


              Well, that's what the user would expect right? If someone explicitly sets it to null, then he should get back null, isn't it?

              • 4. Re: lazily initialize collections in their getters
                jaikiran pai Master

                Coming back to your original question:

                "mars1412" wrote:



                however I have a bad feeling about this - I'm if this will work in all cases with lazy loading, hibernate proxies and stuff I might not even know about.

                What do you think?


                I don't see any problems in this approach, in terms of lazy loading and proxies in Hibernate.

                • 5. Re: lazily initialize collections in their getters
                  mars1412 Apprentice

                  thanks for the answers

                  "jaikiran" wrote:
                  Well, that's what the user would expect right? If someone explicitly sets it to null, then he should get back null, isn't it?


                  Yes, you are right. In the case of collection initalisation the other approach is more appropriate.

                  When I think about it, the lazy initalization approach in the getters came from handling embedded components.
                  As defined by hibernate, the embedded component will be null, if all its elements are null.
                  so hibernate would set the embedded component to null, if all its fields in the database are null.
                  that would break code like this:
                  myEntity().getEmbedded().setEmbeddedField(someValue)

                  In this case this initalization would not help:
                  private EmbeddedComponentTyp embedded = new EmbeddedComponentTyp();





                  • 6. Re: lazily initialize collections in their getters
                    Kamil Demecki Novice

                    It rather question for hibernate forum... forum.hibernate.org

                    With getter are problems. Hibernate check getters if object changed. And when you get object from database and no explicity write, hibernate invoke flush and compare hydrated objects before transaction and after. Getters return other values and hibernate executes additional updates (even you only executes selects - true in standard way but i guess it is configurable with cache mode or something), this may ends with very poor performance with huge amount of updates.