View Full Version : Master-/Detail-Form raises exception
spatz
Jun 8th, 2007, 08:31 AM
Hello
I am working on a billing-managing-project. A billing contains multiple invoice line items.
The data is saved in a database and read with hibernate and spring.
I have tried to create a master-/detail-form on which the user can edit the several invoice line items.
But when I try to open this dialog the following error will be raised:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ch.cadi.inic.saemy.cannonball.business.Billing.ite ms, no session or session was closed
What do I have to do to eliminate this error?
benoitx
Jun 9th, 2007, 03:44 AM
HI
I believe that you have detached objects correct?
One way to get this fixed is to extends the AbstractTableMasterForm an add a method like this:
@Override
protected Class getMasterCollectionType(final ValueModel collectionPropertyVM) {
return Set.class;
}
where you return the collection type that you are using for your property: Set or List.class
This should fix your issue, I think!
Good luck
Benoit
spatz
Jun 9th, 2007, 04:30 AM
Hi, thanks for your reply.
Yes, I have detached objects.
But I do already extend the AbstractTableMasterForm and override this method. (In fact my property is an org.hibernate.collection.PersistentSet)
But the error still occurs...
benoitx
Jun 10th, 2007, 02:32 PM
Try Set.class EVEN if you have a persistent Set.
Do NOT use the persistent classes.
Let me know
B
spatz
Jun 11th, 2007, 12:44 AM
I've tried both and it isn't working with any of this two methods...
Andrius Šabanas
Jun 13th, 2007, 07:17 AM
Hi,
I think this problem is not directly related to Spring RCP, but rather a good old Hibernate detached objects issue.
If you are sending your Billing object through a remoting protocol, like Spring HTTP Invoker or Java RMI, then you need to make sure that your items collection, and probably all item object inside are initialized, that is loaded from the database. For instance, you can call Hibernate.initialize(myObject) to make sure your object proxy gets populated. You need to run this for every item in the collection.
Alternatively, if your application uses Hibernate directly, you could also keep your Hibernate session open until you finish editing your billing object.
hope this helps,
Andrius
spatz
Jun 13th, 2007, 08:15 AM
I connect directly to hibernate (all runs on one pc).
My idea is, that the list of all billings is loaded at the beginning of the program. when the user double-clicks on a billing, a dialog opens with more detailled informations - the master-/detailform.
my hibernate connection is closed long time ago (i dont know anything about the sessionhandling, since i use spring (without rcp ;) ))
is there no other possibility to solve this problem. (am i the only one with this problem?)
Andrius Šabanas
Jun 14th, 2007, 08:28 AM
You need to have at least some basic understanding how Spring handles your Hibernate sessions, because you need to make sure that all collections and entity proxies you will be needing are initialized *before* the session is closed. After the session is gone, your object becomes detached, no further data can be loaded automatically, and you get a LazyInitializationException when you request it.
As I mentioned earlier, you can alternatively have the session open, or reattach the object to another session when the data is needed, although this is more complicated. In any case, this is a Hibernate-specific issue, not Spring RCP.
spatz
Jun 15th, 2007, 04:34 AM
I think I do have some basic understanding how spring works.
What i wanted to say is that i don't know exactly when the sessions are closed and opened and I don't know how to access them.
How can I keep the session opened?
Or where is the right place to reattach the object to the session? In the DAOs? Because I don't like the idea reattaching the object in the view-layer...
How do I do that?
Andrius Šabanas
Jun 18th, 2007, 04:48 AM
Please read these docs, they should give you the necessary understanding:
http://static.springframework.org/spring/docs/2.0.x/reference/orm.html#orm-hibernate
http://www.hibernate.org/hib_docs/v3/reference/en/html/transactions.html
http://www.hibernate.org/hib_docs/v3/reference/en/html/objectstate.html#objectstate-detached
Afterwards, make sure that your objects are properly initialized before returning them outside of transactional code (see my previous posts).
good luck
Andrius
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.