0

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.poze, 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

1
  • 3
    I guess you searched for the tens of questions about this exception in SO, and for the thousands of results in google? Commented Nov 10, 2010 at 19:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Thank you for your help. I'm asking you, there is no other way to do this? Because I don't want to remove the cascading, because if I delete a Transformator, I need to delete the Pictures of that transformator. And I would like to lazy load the object too. So, I can't do, for example, something like: I lazy load the object, but when I need the getPoze(), to make some connection to the DB and take the Pictures?
It cannot save what it cannot read :) Though you can configure it so that it does not cascade on save, but cascade on delete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.