0 Replies Latest reply on Dec 11, 2008 10:43 AM by Sylvain Catudal

    Lazy children loaded when persist is called

    Sylvain Catudal Newbie

      Let's say we have an entity that has a OneToMany relationship declared like this :

      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "Parent")
      public Set<Child> getChildren() {
       return this.children;
      }
      
      public void setChildren(Set<Child> children) {
       this.children = children;
      }
      


      When I call persist on the entity, hibernate firsts populates the list of children although I have never accessed the getter.

      Here is the call in a stateless session bean:
      
      package org.domain.services.users;
      
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import org.domain.SeamOne.entity.Parent;
      import org.domain.services.users.ParentManager;
      
      public @Stateless
      class ParentManagerBean implements ParentManager {
      
       @PersistenceContext
       private EntityManager em;
      
       public void ChangeName(String pName, String pId) {
       Parent parent = em.find(Parent.class, pId);
       // An hibernate trace just appeared that shows a select just for
       // the parent class
      
       parent.setName( pName );
      
      
       em.persist(parent);
       // An hibernate trace just appeared that shows a select to populate
       // the children
       }
      }
      
      


      When I first saw the traces from hibernate, I didn't know for sure which call had produced the traces. I then started jboss in debug mode and stepped through the code to realize that the children were loaded on the persist call.

      My code works fine, but I can't see the need for that query when the children have not been accessed because they can't be dirty.

      I've tried removing the "cascade = CascadeType.ALL" and it then doesn't do the query, but I now have to call persist on the children of the class when they have been modified.

      Is there a reason for that behavior?
      Is that an bug?
      Is there a way to circumvent this without loosing the cascading feature?


      Thank you all,
      Sylvain Catudal