-->
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: lazy=true -> initalize proxy object
PostPosted: Wed Aug 04, 2010 10:08 am 
Newbie

Joined: Wed Jul 29, 2009 9:25 am
Posts: 9
Hello, how are you? I believe what I am asking should be quick and possible to do, as I am sure I've done it before, I just cant find how I did it now.

I have a table 'person' which is linked to the table 'house' and one person has one house. If I fetched the person object from the database, using option lazy=true in configuration, when I try to do person.getHouse(), i will get a proxy object instead of the real House object.

Code:
   
public static Person getPersonById(int id)  {
      Transaction transaction = null;
      Session session = HibernateUtil.getSessionFactory()
               .getCurrentSession();
      transaction = session.beginTransaction();
      Person p = session.get(Person.class, id);
      transaction.commit();
      return p;
}
.....
....
Person p= getPersonById(1);
// in h i will obtain the proxy
House h = p.getHouse();


¿How can I obtain the House object back later? I remember I had to open a session (I closed the session after fetching the person object), and then use Hibernate.initialize(h) or something like this, but I cant get it to work.

Thanks for your time!


Last edited by ykcorse on Wed Aug 04, 2010 11:25 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: lazy=true -> initalizy proxy object
PostPosted: Wed Aug 04, 2010 10:19 am 
Newbie

Joined: Wed Jul 29, 2009 9:25 am
Posts: 9
I found a way to recover the proxy object, I dont know if is the best way though

Code:

public static Person getPersonById(int id)  {
      Transaction transaction = null;
      Session session = HibernateUtil.getSessionFactory()
               .getCurrentSession();
      transaction = session.beginTransaction();
      Person p = session.get(Person.class, id);
      transaction.commit();
      return p;
}
.....
....
Person p= getPersonById(1);
// in h i will obtain the proxy
House h = p.getHouse();

// To obtain the real House object, open session and use Load
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
p=(Person)session.load(Person.class,p.getId());
h=p.getHouse();



This time we have the real object in h since we have not closed the session yet


Top
 Profile  
 
 Post subject: Re: lazy=true -> initalize proxy object
PostPosted: Wed Aug 04, 2010 11:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The much better approach is to load both of these in one query, one communication with the database.

See http://docs.jboss.org/hibernate/core/3. ... e-fetching as well as the sections in HQL and Criteria chapters about fetching, or using fetch profiles (3.5+)


Top
 Profile  
 
 Post subject: Re: lazy=true -> initalize proxy object
PostPosted: Thu Aug 05, 2010 2:29 am 
Newbie

Joined: Wed Jul 29, 2009 9:25 am
Posts: 9
I see your point, you are right.

In this case, I simplified the question, but instead of a house for each person, I have a collection of houses, and the database is huge, so it was important for me to fetch only persons when listing persons (not fetching the colection of houses), and the collection of houses for a person only when I have the need to.


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.