我正在使用Hibernate 3.6.0制作应用程序,并且遇到了LazyInitializationException。我没有设法解决它,所以我在这里寻求您的帮助。

这是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)


Transformator实体的保护程序:

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();
        }
    }


Transformator实体具有一组图片(字节数组)。当我向Transformator实体添加新图片并调用transformator的保护程序时,异常开始。

解决该问题的方法是什么?谢谢

最佳答案

问题在于,要向转换程序添加图片,请调用getPoze()方法,这会使休眠状态尝试从数据库获取集合。显然,它是在没有打开事务的情况下发生的,因此您需要在添加之前关闭事务并在调用saver之后关闭它。像这样:

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();
            }
}


或者,您可以将要添加的集合传递给方法addPoze,以通过减少打开-重新打开事务操作来提高性能。

10-05 18:12