简体   繁体   English

Hibernate抛出org.hibernate.LazyInitializationException

[英]Hibernate threw org.hibernate.LazyInitializationException

After trying to populate javaFX table view with Purchase Object from database using hibernate. 尝试使用Hibernate从数据库中的Purchase Object填充javaFX table view之后。 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. 为了澄清一点,我具有与多对多关系相关的Purchase和Product实体,我已经将此方法(我自己的代码)( 用Extras列休眠了多对多关系 )来映射此关系。 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. 在此代码中,我可以从数据库中获取所有购买商品:请注意,我用thoes元素填充了tableView,对于每个有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) 发生这种情况是因为您试图在Session上下文之外访问这些lineItems集合,并且这些lineItems对象不是您用于获取购买商品的实际查询所带来的

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 休眠代码中用于选择“购买”项目的某个位置,您应该为这些lineItem添加获取联接模式

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 然后将您的列表包装到此调用之外的另一个ObservableList中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法抛出“org.hibernate.LazyInitializationException”异常 - Method threw 'org.hibernate.LazyInitializationException' exception Hibernate ManyToMany 方法抛出“org.hibernate.LazyInitializationException”异常 - Hibernate ManyToMany Method threw 'org.hibernate.LazyInitializationException' exception Hibernate org.hibernate.LazyInitializationException:无法初始化 - Hibernate org.hibernate.LazyInitializationException: could not initialize org.hibernate.LazyInitializationException(Spring / Hibernate) - org.hibernate.LazyInitializationException (Spring/Hibernate) 事务内的org.hibernate.LazyInitializationException - org.hibernate.LazyInitializationException within Transaction org.hibernate.LazyInitializationException关联集合 - org.hibernate.LazyInitializationException associated collection 在TestNGTests上获取org.hibernate.LazyInitializationException - Getting org.hibernate.LazyInitializationException on a TestNGTests 如何解决org.hibernate.LazyInitializationException? - how can to resolve org.hibernate.LazyInitializationException? (org.hibernate.LazyInitializationException)org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - (org.hibernate.LazyInitializationException) org.hibernate.LazyInitializationException: could not initialize proxy - no Session Spring和Hibernate Web应用程序:如何解决&#39;org.hibernate.LazyInitializationException? - Spring and Hibernate web application: how to resolve 'org.hibernate.LazyInitializationException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM