-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Children collection initialized and lazy=true
PostPosted: Tue Mar 24, 2009 4:54 pm 
Newbie

Joined: Tue Mar 24, 2009 4:30 pm
Posts: 5
Hi,

First of all, I'm trying to persist hierarchical data via hibernate. I have a class, AbstractHierarchy that has a collection of children, also of type AbstractHierarchy. There are two concrete subclasses of this, ConcreteHierarchy1 and ConcreteHierarchy2. Which one is instantiated depends on a discriminator value.

I'm having a problem where between one revision of my codebase and the next the lazy collection, "children," seems to be automatically initialized. That is, in revision 1, everything works as expected. In revision 2, as soon as Query.list() returns, before any other code executes, if I drop a breakpoint and examine the object in the result set, the children colleciton is fully initialized and the initialized flag is true on the collection.

Between check-ins no changes whatsoever were made to the mapping file. Here are some details:

Hibernate version: 3.2.5.ga

Relevant portion of mapping document

Code:

<hibernate-mapping auto-import="true" default-lazy="true">

  ...

  <class

name="xxx.hierarchy.impl.AbstractHierarchy"

    abstract="true" table="Hierarchies">

    <id name="id" column="Id" type="string">

      <generator class="assigned" />

    </id>

    ... discriminator some other properties ...

    <bag name="children" table="Hierarchies" lazy="true"

      cascade="all-delete-orphan">

      <key column="ParentId" />

      <one-to-many

class="xxx.hierarchy.impl.AbstractHierarchy" />

    </bag>

    <subclass name="xxx.hierarchy.impl.ConcreteHierarchy1"

      discriminator-value="false">

    </subclass>

    <subclass

      name="xxx.hierarchy.impl.ConcreteHierarchy2"

      discriminator-value="true">

      ... some other properties ...

      </bag>

    </subclass>

  </class>

  ...

</hibernate-mapping>



...and this is the relevant portion of the current version of the classes:

Code:

public abstract class AbstractHierarchy extends ... implements HierarchySimpleInterface, ...

{

private Collection<? extends AbstractHierarchy> _children;

/*

   * (non-Javadoc)

   *

   * @see xxx.hierarchy.Hierarchy#getChildren()

   */

  public Collection<? extends HierarchySimpleInterface> getChildren() {

    return _children;

  }

  /**

   * @param children_

   *          the children to set

   */

  public final void setChildren(

      List<? extends HierarchySimpleInterface> children_) {

    if (null != children_) {

      for (HierarchySimpleInterface current : children_) {

        assert (current.getClass() == getClass());

      }

    }

    @SuppressWarnings("unchecked")

    List<? extends AbstractHierarchy> castChildren = (List<? extends AbstractHierarchy>) children_;

    _children = castChildren;

  }

}

public final class ConcreteHierarchy1 extends AbstractHierarchy {

}

public class ConcreteHierarchy2 extends AbstractHierarchy

    implements HierarchyComplexInterface, ...

{

/*

   * (non-Javadoc)

   *

   * @see xxx.hierarchy.Hierarchy#getChildren()

   */

  @Override

  public final Collection<HierarchyComplexInterface> getChildren() {

    // data constraints ensure that all children of a hierarchy will be of the

    // same concrete type as the parent

    @SuppressWarnings("unchecked")

    Collection<HierarchyComplexInterface> result = (Collection<HierarchyComplexInterface>) super

        .getChildren();

    return result;

  }

}



here is the relevant part of the diff between version 1 and version 2:

Code:

-public abstract class AbstractHierarchy<IMPLTYPE extends HierarchySimpleInterface<IMPLTYPE>>

-    implements HierarchySimpleInterface<IMPLTYPE>, ... {

+public abstract class AbstractHierarchy

+    ... implements HierarchySimpleInterface,

+    ... {

-  private Collection<IMPLTYPE> _children;

+  private Collection<? extends AbstractHierarchy> _children;


-  public final Collection<IMPLTYPE> getChildren() {

+  public Collection<? extends AbstractHierarchy> getChildren() {

     return _children;

   }

-  public final void setChildren(List<IMPLTYPE> children_) {

+  public final void setChildren(

+      List<? extends AbstractHierarchy> children_) {

+    if (null != children_) {

+      for (AbstractHierarchy current : children_) {

+        assert (current.getClass() == getClass());

+      }

+    }

     _children = children_;

   }



Basically the class took itself as a template parameter, which was much more trouble than it was worth and that's why it was eliminated.

I'm having trouble understanding how a change to the source without a change to the mapping file could result in this behavior. I've read how sometimes you can have issues with mapping lazy associations to abstract classes; I dont think that applies here, but regardless, both HierarchyComplexInterface and HierarchySimpleInterface above are interfaces.

some additional info:

Name and version of the database you are using: Oracle 10g (using org.hibernate.dialect.Oracle9Dialect)

Suspicious Hibernate log excerpt:

[[xxx.hierarchy.impl.AbstractHierarchy.children#57ea638852bc4e0b91e5debe8473bd18]]

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.CollectionLoadContext] - collection not yet initialized; initializing

Here is a larger excerpt from the log:

2009-03-24 19:46:25,003 DEBUG [xxx.dao.hibernate.HibernateIdentifiableDao] - searching for objects of type [AbstractHierarchy] on [0] criteria

2009-03-24 19:46:25,003 TRACE [org.hibernate.engine.query.QueryPlanCache] - unable to locate HQL query plan in cache; generating (select _obj_ from xxx.hierarchy.impl.AbstractHierarchy _obj_ order by _obj_.type.name ASC)

2009-03-24 19:46:25,019 TRACE [org.hibernate.engine.query.HQLQueryPlan] - HQL param location recognition took 0 mills (select _obj_ from xxx.hierarchy.impl.AbstractHierarchy _obj_ order by _obj_.type.name ASC)

2009-03-24 19:46:25,019 TRACE [xxx.dao.hibernate.HibernateIdentifiableDao] - querying for objects of type [AbstractHierarchy] with HQL [select _obj_ from xxx.hierarchy.impl.AbstractHierarchy _obj_ order by _obj_.type.name ASC] (pageSize = [1], page = [-1], params = [])

2009-03-24 19:46:25,019 TRACE [org.hibernate.engine.query.QueryPlanCache] - located HQL query plan in cache (select _obj_ from xxx.hierarchy.impl.AbstractHierarchy _obj_ order by _obj_.type.name ASC)

2009-03-24 19:46:25,019 TRACE [org.hibernate.engine.query.HQLQueryPlan] - find: select _obj_ from xxx.hierarchy.impl.AbstractHierarchy _obj_ order by _obj_.type.name ASC

2009-03-24 19:46:25,019 TRACE [org.hibernate.engine.QueryParameters] - named parameters: {}

2009-03-24 19:46:25,034 DEBUG [org.hibernate.engine.TwoPhaseLoad] - resolving associations for [xxx.hierarchy.impl.ConcreteHierarchy1#57ea638852bc4e0b91e5debe8473bd18]

2009-03-24 19:46:25,034 TRACE [org.hibernate.engine.loading.LoadContexts] - creating collection wrapper:[xxx.hierarchy.impl.AbstractHierarchy.collection2#57ea638852bc4e0b91e5debe8473bd18]

2009-03-24 19:46:25,050 DEBUG [org.hibernate.engine.TwoPhaseLoad] - resolving associations for [xxx.Type1#9d258a5ff07d479c91d03841ec02b9ad]

2009-03-24 19:46:25,050 DEBUG [org.hibernate.engine.TwoPhaseLoad] - done materializing entity [xxx.Type1#9d258a5ff07d479c91d03841ec02b9ad]

2009-03-24 19:46:25,050 DEBUG [org.hibernate.engine.TwoPhaseLoad] - resolving associations for [xxx.Type2#ef2b7d67415c4709b69281ce383d1cf9]

2009-03-24 19:46:25,050 DEBUG [org.hibernate.engine.TwoPhaseLoad] - done materializing entity [xxx.Type2#ef2b7d67415c4709b69281ce383d1cf9]

2009-03-24 19:46:25,050 DEBUG [org.hibernate.engine.TwoPhaseLoad] - resolving associations for [xxx.hierarchy.Type3#b8ad2a9a050e4924a8e94e1a0b32d2bb]

2009-03-24 19:46:25,050 TRACE [org.hibernate.engine.loading.LoadContexts] - creating collection wrapper:[xxx.hierarchy.Type3.collection1#b8ad2a9a050e4924a8e94e1a0b32d2bb]

2009-03-24 19:46:25,050 TRACE [org.hibernate.engine.loading.LoadContexts] - creating collection wrapper:[xxx.hierarchy.Type3.collection2#b8ad2a9a050e4924a8e94e1a0b32d2bb]

2009-03-24 19:46:25,050 DEBUG [org.hibernate.engine.TwoPhaseLoad] - done materializing entity [xxx.hierarchy.Type3#b8ad2a9a050e4924a8e94e1a0b32d2bb]

2009-03-24 19:46:25,050 TRACE [org.hibernate.engine.loading.LoadContexts] - creating collection wrapper:[xxx.hierarchy.impl.AbstractHierarchy.collection3#57ea638852bc4e0b91e5debe8473bd18]

2009-03-24 19:46:25,050 TRACE [org.hibernate.engine.loading.LoadContexts] - creating collection wrapper:[xxx.hierarchy.impl.AbstractHierarchy.children#57ea638852bc4e0b91e5debe8473bd18]

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.LoadContexts] - constructing collection load context for result set [org.apache.commons.dbcp.DelegatingResultSet@169eec2]

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.CollectionLoadContext] - starting attempt to find loading collection [[xxx.hierarchy.impl.AbstractHierarchy.children#57ea638852bc4e0b91e5debe8473bd18]]

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.CollectionLoadContext] - collection not yet initialized; initializing

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.CollectionLoadContext] - starting attempt to find loading collection [[xxx.hierarchy.impl.AbstractHierarchy.children#57ea638852bc4e0b91e5debe8473bd18]]

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.LoadContexts] - attempting to locate loading collection entry [CollectionKey[xxx.hierarchy.impl.AbstractHierarchy.children#57ea638852bc4e0b91e5debe8473bd18]] in any result-set context

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.LoadContexts] - collection [CollectionKey[xxx.hierarchy.impl.AbstractHierarchy.children#57ea638852bc4e0b91e5debe8473bd18]] not located in load context

2009-03-24 19:46:25,066 TRACE [org.hibernate.engine.loading.CollectionLoadContext] - found loading collection bound to current result set processing; reading row

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 3:15 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
    if (null != children_) {
      for (HierarchySimpleInterface current : children_) {
        assert (current.getClass() == getClass());
      }
    }


This part of your code is guaranteed to initialize the collection.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.