-->
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: LazyInitializationException and Interface
PostPosted: Tue Jul 13, 2004 6:43 pm 
Newbie

Joined: Thu Jun 17, 2004 6:31 am
Posts: 5
Hi

I've read some threads about LazyInitializationException. However, something I'm still curious about is: Why throwing an exception instead of returning null.

Background for this question:

I've got a recursive object (tree-like structure) containing a parent property.

Code:
<hibernate-mapping package="ch.jfactory.pm.data">
    <class name="ch.jfactory.pm.data.modelapi.IFEntry" table="ENTRY">
        <id name="id" type="integer" unsaved-value="null">
            <generator class="identity"/>
        </id>
        <discriminator column="discriminator"/>
        <property name="text" type="string"/>
        <property name="timestamp" type="timestamp"/>
        <property name="rank" type="integer"/>
        <property name="startDate" type="timestamp"/>
        <property name="endDate" type="timestamp"/>
        <many-to-one name="parent" class="ch.jfactory.pm.data.modelapi.IFEntry"/>
        <many-to-one name="type" class="ch.jfactory.pm.data.model.DefaultType"/>
        <many-to-one name="work" class="ch.jfactory.pm.data.model.DefaultWork"/>
        <set name="children" inverse="true" order-by="rank asc" cascade="all" >
            <key column="PARENT"/>
            <one-to-many class="ch.jfactory.pm.data.modelapi.IFEntry"/>
        </set>
        <subclass name="ch.jfactory.pm.data.model.DefaultEntry"
                  discriminator-value="default"/>
    </class>
</hibernate-mapping>

IFEntry is an interface and I simply want to have one implementing class for it (DefaultEntry). So I have choosen the table-per-class-hierarchy approach. Before I added the subclass element (before I was implementing the interface), the following code worked perfectly:

Code:
public void setParent(IFEntry parent) {
    if (this.parent != parent) {

        // Set the parent
        final IFEntry oldParent = this.parent;
        this.parent = parent;

        // Make sure the old parent does not contain this entry any more
        if (oldParent != null && oldParent.getChildren().contains(this)) {
            oldParent.deleteChild(this);
        }

        // Make sure the parent contains this entry
        final Set children = parent.getChildren();
        if (parent != null && children != null && !children.contains(this)) {
            children.add(this);
        }
    }
}

Since I started to use the interface and the subclass element, the same code fails to load the children set and I get a LazyInitializationException. Now I am catching this exception and initializing the children objects in the catch block. This is ugly and I would appreciate to have a better solution to it.
I am seduced to believe that returning null instead of throwing an exception would be more user-friendly, at least from the point of view of the above code.
On the other hand I don't understand why switching to the interface is an issue.
The above code is called actually in a session:

Code:
final Session session = HibernateUtils.currentSession();
list = query(session, "IS NULL");
HibernateUtils.closeSession();


Details: Hibernate 2.1.4, HSQLDB 1.7.2rc6

Thanks for any ideas on the backgrounds.

_________________
Thanks for your help.
Daniel Frey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 4:53 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
Just call Hibernate.initialize(children) ?


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.