LazyInitializationException问题

时间:2010-11-10 19:57:09

标签: java hibernate

我正在使用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实体添加新图片并为变换器调用保护程序时,异常就开始了。

这个问题的解决方案是什么?感谢

2 个答案:

答案 0 :(得分:1)

问题是要向transformator添加图片,你调用getPoze()方法,这使得hibernate尝试从数据库中获取集合。显然它没有打开一个事务就会发生,所以你需要的是在调用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方法传递要添加的集合,以通过减少开放重新打开的事务操作来提高性能。

答案 1 :(得分:1)

您的transformator对象包含一个先前从hibernate获取的未完全加载的集合(延迟加载)。当你持久化时,你已经触发了级联到该对象,然后当它尝试读取未加载的数据时(由于延迟加载),你会得到一个异常。

删除级联(如果这是你想要的)或者通过在获取Transformator对象时急切加载/显式调用该集合的getter以加载它来完全加载对象。