-->
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: Need explanation
PostPosted: Sun Jul 27, 2008 12:12 pm 
Newbie

Joined: Sun Jul 27, 2008 10:56 am
Posts: 17
Hi all,

Hibernate version:
3

Mapping documents:
I have a one to one association between class A and class B, the only direction for me is from A to B

Code between sessionFactory.openSession() and session.close():
Code:
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
           
List aList = session.createQuery("from A").list();

session.getTransaction().commit();

String desc = ((A)aList.get(0)).getB().getDescription();
}
catch(Exception e) {HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();           
}       


Full stack trace of any exception that occurs:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
....
Name and version of the database you are using:
MySql 5


My object A has a method to retrieve its associated object B, that is getB()
I have noticed that if I access a property of an object B after session commit where I loaded all objects A, I have that above exception.

I have also noticed that, If I call also following statement:
List bList = session.createQuery("from B").list();
after loading of A objects, then I can access all properties of all related B objects.

What I would like to understand is if there are other clever methods to load an Object and all its related object, or If I have to load all objects inside a Session.

Thanks in advance,
best regards
Gamby


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 7:11 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
What I would like to understand is if there are other clever methods to load an Object and all its related object, or If I have to load all objects inside a Session.


Yup, everything has to be loaded within an open session, otherwise, the associated objects are just proxies, and when you access them, you will have no active connection or session, creating a LazyInitializationException.

Using longer living Sessions, by employing something like the Open Session In View Pattern, will help you avoid LazyLoadingException problems.

You might find this tutorial on the inner workings of Hibernate somewhat enlightening. Check it out!

How Hibernate Works

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Jul 28, 2008 9:01 am 
Newbie

Joined: Sun Jul 27, 2008 10:56 am
Posts: 17
Thank you very much,
I'll read that tutorial as soon as possible.

Best regards
Gamby


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 02, 2008 12:28 pm 
Newbie

Joined: Sun Jul 27, 2008 10:56 am
Posts: 17
Hi Cameron,

I read your suggested article, but really it didn't help me to understand and solve m problem.

Let me you know that I'm not in a web context, that is I'm working on a rich client application with a local dbms, MySql.

The context in which I have that exception is:
- When app starts load objects A inside a transcation obviuosly, then commit
- A User can double click on one of those A objects to edit it.
- In this case, a dialog should appear showing all object A properties and all B object properties. remember that B is in one to one association with A.

I have that exception when, during dialog contents creation, I read object B properties to fill dialog widgets.

During dialog creation, there is no section and in general no hibernate concept involved.

After User click on ok button on dialog, then real update/insert happens.

Please, advise me about a solution to adopt for my specific context...I'm reading many tutorials and articles, but I really don't understand...

Thanks, best regards
Gamby


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.