Problem with LazyInitializationException
I'm making an application using Hibernate 3.6.0, and I got a LazyInitializationException. I didn't manage to resolve it, so I'm here, asking for your help.
Here is the StackTrace:
Exception in thread "AWT-EventQueue-0" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.Transformator.poz开发者_Python百科e, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212)
at model.Transformator.addPoza(Transformator.java:93)
at controller.WinController.uploadPoza(WinController.java:47)
at view.Win$1UploadActionListener.actionPerformed(Win.java:138)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The saver for Transformator entity:
public void transformatorSaver(Transformator t)
{
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
@SuppressWarnings("unused")
Transformator tr = (Transformator) session.merge(t);
tx.commit();
}
catch (Exception e)
{
tx.rollback();
}
finally
{
session.close();
}
}
The Transformator entity has a set of pictures (byte array). The exception starts when I add a new picture to the Transformator entity and call the saver for transformator.
Which is the resolution for this poblem? Thanks
The problem is that to add a picture to transformator you call getPoze() method, which makes hibernate try to obtain the collection from database. Obviously it happens without a transaction opened, so what you need is to begin a transaction before adding and close it after calling saver. Something like this:
public void addPoze(Transformator tr, Poze poze) {
try
{
tx = session.beginTransaction();
tr.getPoze().add(poze);
@SuppressWarnings("unused")
Transformator tr = (Transformator) session.merge(t);
tx.commit();
}
catch (Exception e)
{
tx.rollback();
}
finally
{
session.close();
}
}
or you can pass to method addPoze a collection you want to add to increase the performance by reducing open-reopen transaction operations.
Your transformator object contains a collection that was previously obtained from hibernate that aren't fully loaded (lazy load). When you persist you have triggered cascading to that object and when it then tries to read the data that was not loaded (due to lazy load) you get an exception.
Either remove the cascading (if that is what you want) or load the object fully by eager loading/explicit calling the getter for that collection to load it when you obtain the Transformator object.
精彩评论