简体   繁体   中英

Hibernate threw org.hibernate.LazyInitializationException

After trying to populate javaFX table view with Purchase Object from database using hibernate. To clarify thing a little, i have Purchase and Product entities related with many to many relationship, i have applied this methode [my own code] ( Hibernate many to many relationship with extras columns ) to map this relationship. I get this error:

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.Purchase.lineItems, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:567)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:205)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:546)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:133)
    at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:163)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:343)
    at javafx.collections.ModifiableObservableListBase.addAll(ModifiableObservableListBase.java:99)
    at controller.purchase.PurchaseController.setPurchaseHeader(PurchaseController.java:254)
    at controller.purchase.PurchaseController.btnEditClicked(PurchaseController.java:244)

THIS THE CODE WHERE I GET ALL PURCHASE FROM DATABASE: Notice that i fill a tableView with thoes element, and for each Purchase with have LineItems.

 public ObservableList<Purchase> findAll() {
        try {
            if (!session.isOpen())
                session = DatabaseUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria c = session.createCriteria(Purchase.class);
            c.setFetchMode("lineItems", FetchMode.JOIN);
            Query query = session.createQuery("  from Purchase ");
            ObservableList<Purchase> list = FXCollections.observableArrayList(query.list());
            session.getTransaction().commit();
            session.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

It happens because you are trying to access these lineItems collection outside a Session context, and these lineItems objects were not brought by the actual query you used to fetch the Purchase(s)

Somewhere in you hibernate code that does the selection for your Purchase items you are supposed to add a fetch join mode for these lineItems

Like :

Criteria c = sessionFactory.getCurrentSession().createCriteria(Purchase.class);
c.setFetchMode("lineItems", FetchMode.JOIN);

EDIT

After reading your code, I'd suggest something like:

public List<Purchase> findAll() {
   try {
        if (!session.isOpen())
            session = DatabaseUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Purchase.class);
        c.setFetchMode("lineItems", FetchMode.JOIN);
        List<Purchase> list = (List<Purchase>)c.list();

        session.close();
        return list;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

And then wrap your List into another ObservableList outside this call call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM