【问题标题】:Hibernate initialize object in another session休眠在另一个会话中初始化对象
【发布时间】:2016-08-23 18:53:24
【问题描述】:

我有一个非常简单的实体对象用户,它与用户角色有关。困扰我的是,当我从数据库中获取用户对象并将其传递给另一个想要访问该用户对象中的用户角色的方法时,我得到了著名的无法初始化代理休眠异常。

例如。方法一:

- open session, open transaction
- fetch user 
- close transaction, close session

现在将该用户对象传递给方法 2

- open session, open transaction
- do Hibernate.initialize(user.getUserRole()) --> here it breaks
- close transaction, close session

我猜这是一些 Hibernate 功能,您无法在新会话中初始化对象,但我不知道为什么。有什么解决办法吗?

PS。我已经阅读了有关<property name="hibernate.enable_lazy_load_no_trans">true</property> 的信息,但这看起来像是正常的事情,并且认为没有必要打开此选项。

编辑:代码示例:

用户类的getter和setter

@ManyToOne(fetch = FetchType.LAZY, targetEntity = UserRoles.class)
@JoinColumn(name = "USER_ROLE_ID", nullable = false)
public UserRoles getUserRoles() {
    return this.userRoles;
}

public void setUserRoles(UserRoles userRoles) {
    this.userRoles = userRoles;
}

用户角色的字段

 private Integer id;
 private String description;
 private Character active;
 private Set userses = new HashSet(0);

方法一

public static void method1() {
    Users user = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String hqlQuery = "FROM Users WHERE userName = :userName";
        Query query = session.createQuery(hqlQuery);
        query.setParameter("userName", userName);
        List<Users> list = query.list();    
        for (Users u : list) {
            user = u;
            break;
        }
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        Logging.handleExceptionLogging(e);
    } finally {
        session.close();
    }

    method2(user);
}

方法二

public static method2(Users user) {

if (user != null) {

        if (Hibernate.isInitialized(user.getUserRoles())) {
            if (user.getUserRoles().getDescription() != null && user.getUserRoles().getDescription().toLowerCase().equals("admin"))
                returnValue = true;
        } else {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                Hibernate.initialize(user.getUserRoles()); // this line causes Exception
                if (user.getUserRoles().getDescription().toLowerCase().equals("admin"))
                    returnValue = true;
                tx.commit();
            } catch (HibernateException e) {
                if (tx != null)
                    tx.rollback();
                Logging.handleExceptionLogging(e);
            } finally {
                session.close();
            }
        }
    }
}

【问题讨论】:

  • 也许发布您的实体以及您如何调用获取?
  • 我提供了代码示例(大部分),tnx

标签: java hibernate session


【解决方案1】:

所以基本上经过大量谷歌搜索和阅读有关会议的信息后,我的假设是正确的。实体对象几乎“绑定”到它们被提取的原始会话,并且不能以这种方式执行这样的查询。一种解决方案是将实体绑定到调用 session.update(entity) 的新会话,并且这个新会话知道实体。基本上发出新查询以再次填充实体字段。因此,如果没有必要,请避免这种情况,并尝试在原始会话中获取所有需要的数据。

更多阅读:Could not initialize proxy - no Session

【讨论】:

    猜你喜欢
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多