【问题标题】:How to receive the entity without LazyInitializationException?如何在没有 LazyInitializationException 的情况下接收实体?
【发布时间】:2015-08-20 08:08:52
【问题描述】:

我有两个实体:

一个 id bigint auto_inctement b_id 大整数 乙 id 大整数 日期时间戳

和代码:

public void test2() {
    B b = getBByA(3l);
    System.out.println(b.getDate()); // <--- lazy initialization exception
}

public Revision getBByA(long a_id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        A a = (A) session.get(A.class, a_id);

        return a.getB();
    } catch (RuntimeException e) {
        e.printStackTrace();
        throw e;
    } finally {
        session.close();
    }
}

如何正确接收 b.getDate()? (没有lazy="eager")

【问题讨论】:

    标签: hibernate lazy-initialization


    【解决方案1】:

    当您在会话关闭后尝试获取数据时,您可以尝试使用:

    Hibernate.initialize(b.getDate())
    

    这里有更多细节http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-fetching-initialization

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2011-01-04
      相关资源
      最近更新 更多