-->
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.  [ 4 posts ] 
Author Message
 Post subject: who to avoid fetching children
PostPosted: Sat Nov 24, 2007 12:51 am 
Newbie

Joined: Fri Mar 09, 2007 6:23 am
Posts: 3
Location: Lahore
this is my hbm file
Code:
<hibernate-mapping>
    <class name="com.model.AccountImpl" table="account">
        <id name="accountId" column="id">
            <generator class="increment" />
        </id>
        <property name="name" column="name" />
        <property name="description" column="description" />
        <property name="active" column="active" />
        <property name="code" column="code" />       
       
        <many-to-one name="parentAccount"  column="parentId" class="com.model.AccountImpl"/>
       
        <set name="children" cascade="save-update"
            inverse="true" >
            <key column="parentId"/>
            <one-to-many class="com.model.AccountImpl"/>
        </set>         
       
    </class>
</hibernate-mapping>


the description is AccountImpl class is mapped with a table named account having properties
parantAccount of type Account (in the same table )
set of accounts as children

in table some columns are
id is primary key
parentId is foreign key to the same table as this table have accounts that can be a parent account or child of some other account

thats why i put an object parentAccount of type AccountImpl and a set children having objects of AccountImpl in AccountImpl as hbm dipicts all


the issue is if from within application i call this with lazy="false" it fetches all of the records in account table coz of parent child relationship

but when i set lazy="true" or doest not set the lazy attribute.
from within application call it works but when i call the same function from dwl it throws an exception 'session closed or no session is available'

coz i am trying to get children on the basis of parentCode as

Code:
Session s = hibernateTemplate.getSessionFactory().openSession();
        child = s.createCriteria(Account.class)
                .createAlias("parentAccount","p")
                .add(Restrictions.eq("p.code",code)).list();
       
       
        s.close();

the exception is

Code:
ERROR [org.hibernate.LazyInitializationException] - failed to lazily initialize a collection of role: com.model.AccountImpl.children, no session or session was closed


as i m using hibernate with spring so i m using this filter
as
Code:
<filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>com.aedsys.service.FlushingSpringSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 3:07 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi Curious,

Error comes because, you try to get the child list after the session is closed.
Initialize your childs object manually, if you are using Criteria,
i.e)

Session s = hibernateTemplate.getSessionFactory().openSession();
child = s.createCriteria(Account.class)
.createAlias("parentAccount","p")
.add(Restrictions.eq("p.code",code)).list();

Hibernate.intialize(account.getChildren());
s.close();

Hope this helps!!!!

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 6:08 am 
Newbie

Joined: Fri Mar 09, 2007 6:23 am
Posts: 3
Location: Lahore
Code:
Session s = hibernateTemplate.getSessionFactory().openSession();
child = s.createCriteria(Account.class)
.createAlias("parentAccount","p")
.add(Restrictions.eq("p.code",code)).list();

Hibernate.intialize(account.getChildren());

as in ur given suggestion
initialize is a function of hibernateTemplate but how can i pass account.getChildren() to it as i hvnt any 'account' here

please explain the idea u mentioned here


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 3:03 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi Curious,

In the class Hibernate, you have a method called initialize(Arg), Pass your collections which want to load....I'm not talking about your Spring reference variable hibernateTemplate

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.