-->
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.  [ 1 post ] 
Author Message
 Post subject: Bag/Set behavior with hierarchy table
PostPosted: Tue Jan 09, 2007 7:16 am 
Newbie

Joined: Sun Apr 10, 2005 6:53 am
Posts: 4
Hibernate version:

Hibernate 3.2

Mapping documents:

Code:
<class name="ProjectItem" table="LM_PROJECT_ITEM" polymorphism="explicit">

        <id name="id" column="M_ID" >
         <generator class="uuid"/>
      </id>

        <property name="name" column="M_NAME" not-null="true"/>
        <property name="description" column="M_DESCRIPTION" not-null="true"/>
        <property name="date" column="M_CREATION_DATE" type="date"/>
        <property name="parentId" column="M_PARENT_ID" />

        <bag name="childs">
            <key column="M_PARENT_ID"/>
            <one-to-many class="com.asv.sdi.lm.model.ProjectItem"/>
        </bag>

        <join table="LM_EX_PROJECT_ITEM" optional="true" inverse="true">
            <key column="EX_ID"/>
            <property name="fk" column="EX_ID" insert="false" update="false"/>
            <property name="howToTest" column="M_HOW_TO_TEST"/>
            <property name="importance" column="M_IMPORTANCE"/>
            <property name="initialEstimate" column="M_INITIAL_ESTIMATE"/>
            <property name="realEstimate" column="M_REAL_ESTIMATE"/>
            <property name="progress" column="M_PROGRESS"/>
        </join>

   </class>

DAO code:

I am using Spring2

Version 1
Code:
public List getRootProjectItems() {
        List items = getHibernateTemplate().find("from ProjectItem as pi left outer join fetch pi.childs where pi.parentId is null ");
        return items;
    }


Version 2
Code:
public List getRootProjectItems() {
        List items = getHibernateTemplate().find("from ProjectItem as pi where pi.parentId is null ");
        if (items != null) {
            Iterator it = items.iterator();
            while (it.hasNext()) {
                ProjectItem projectItem = (ProjectItem) it.next();
                getHibernateTemplate().initialize(projectItem.getChilds());
            }
        }
        return items;
    }


Hierarchy level is unrestricted. It works OK with Set Collection for childs.

Code:
   <set name="childs">
            <key column="M_PARENT_ID"/>
            <one-to-many class="com.asv.sdi.lm.model.ProjectItem"/>
        </set>


But it doesn't work for Bag. I have got an Exception:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.ProjectItem.childs, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:249)


Test Code

Code:
public void testAllItems() {
        List rootProjectItems = projectItemDAO.getRootProjectItems();
        for (int i = 0; i < rootProjectItems.size(); i++) {
            ProjectItem projectItem = (ProjectItem) rootProjectItems.get(i);
            BigDecimal initialEstimate = projectItem.getInitialEstimate();
            showCurrentHierarchyLevel(projectItem.getChilds());
        }
    }

    private void showCurrentHierarchyLevel(Collection childs) {
        Iterator it = childs.iterator();
        while (it.hasNext()) {
            ProjectItem projectItem = (ProjectItem) it.next();
            if (projectItem.getChilds() != null) {
                showCurrentHierarchyLevel(projectItem.getChilds());
            }
        }
    }

Could you explain me strange behavior of Bag? What is the source of this error?


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.