簡體   English   中英

LazyInitializationException問題

[英]Problem with LazyInitializationException

我正在使用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()方法,這使得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,以通過減少打開-重新打開事務操作來提高性能。

您的transformator對象包含一個以前從未從休眠中獲取的,尚未完全加載(延遲加載)的集合。 當您持久化時,您已觸發了對該對象的級聯,然后當它嘗試讀取未加載的數據(由於延遲加載)時,您將獲得異常。

要么刪除級聯(如果您想要的話),要么通過急於加載/明確調用該集合的getter來在您獲取Transformator對象時加載該對象,從而完全加載該對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM