-->
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.  [ 3 posts ] 
Author Message
 Post subject: Newbie Question regarding LazyInitializationException
PostPosted: Fri Apr 21, 2006 12:10 pm 
Newbie

Joined: Fri Apr 21, 2006 11:53 am
Posts: 3
Ok before I break out all the exceptions and code I just want to make sure I understand how Hibernate is supposed to work.

I have an object Company that has many Portfolios, in the Company.hbm.xml it is mapped

<set name="portfolioSet" inverse="true">
<key column="companyID"/>
<one-to-many class="Portfolio"/>
</set>

now when I retrieve a Company object in my DAO

public Company getCompany(Integer id){
Session session = null;

try {
session = SessionFactory.currentSession();

List companyList = session.createCriteria(com.v360.hibernate.model.Company.class)
.add(Expression.eq("id", id))
.list();
return (Company) companyList.get(0);
}
catch (HibernateException e){

throw new RuntimeException(e);
}
finally{
if (session != null)
{
try {
session.close();
}
catch (HibernateException e){
throw new RuntimeException(e);
}
}
}

}

The Company object properities get populated with the appropriate values. How ever when I try to iterate through the Set of Portfolio's I get an exception. Now my question is this, am I suppose to add code to my DAO to populate the Set of Portfolios belonging to a Company or is Hibernate suppose to do this on the retrieveal of the Company?

If Hibernate is suppose to then I'll post my code and all my exceptions.

Regards,

JR


Top
 Profile  
 
 Post subject: Newbie error
PostPosted: Fri Apr 21, 2006 2:08 pm 
Newbie

Joined: Fri Apr 21, 2006 11:53 am
Posts: 3
I was closing the session, in my CompanyDAO. Evidently the Set of Portfolios isn't populated until I try and iterate through the Set.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 2:16 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
You can use Hibernate.initialize(company.getPortfolioSet()) to initialize the collection while the session is still open. Then you can pass the Company object to whatever layer you want without needing to maintain an open session.

Curtis ...

_________________
---- 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.  [ 3 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.