简体   繁体   English

从 Spring 3 迁移到 Spring 4 时的 org.hibernate.HibernateException

[英]org.hibernate.HibernateException when migrating from Spring 3 to Spring 4

I am migrating an application from Spring 3 to Spring 4. In particular, I am migrating it to我正在将应用程序从 Spring 3 迁移到 Spring 4。特别是,我将它迁移到

    <spring.version>4.2.9.RELEASE</spring.version>
    <org.springframework.ws.version>3.0.8.RELEASE</org.springframework.ws.version>       
    <hibernate.version>4.3.11.Final</hibernate.version>

Here is the original code that worked with Spring 3:这是与 Spring 3 一起使用的原始代码:

public AcmeUserBean loadUserByDn(String userDn) {
    AcmeUserBean result = null;
    Session session = null;
    try {
        session = this.currentSession();

        Transaction tx = session.beginTransaction();

        Query query = session.createQuery(
            "from AcmeUserBean as user where user.distinguishedName = :userDn");
        query.setString("userDn", userDn);
        List objs = query.list();

        if (objs != null && objs.size() > 0) {
            result = (AcmeUserBean) objs.get(0);
        }

        tx.commit();
        session.close();
        return result;
    } finally {
        if (session != null) {
            session.flush();
            session.close();
        }
    }

}

With Spring 4, I updated it to:在 Spring 4 中,我将其更新为:

@Transactional
public AcmeUserBean loadUserByDn(String userDn) {
    AcmeUserBean result = null;
    Session session = null;
    try
    {
        session = this.getSessionFactory().getCurrentSession();

        Query query = session.createQuery(
            "from AcmeUserBean as user where user.distinguishedName = :userDn");
        query.setString("userDn", userDn);
        List objs = query.list();

        if (objs != null && objs.size() > 0) {
            result = (AcmeUserBean) objs.get(0);
        }

        return result;
    } finally {
        if (session != null) {
            session.flush();
            session.close();
        }
    }
}

Each line in the method succeeds and result is not null at the return line.方法中的每一行都成功并且result在返回行不为空。 However, an exception occurs when the method exits:但是,该方法退出时会出现异常:

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; org.springframework.transaction.TransactionSystemException: 无法提交 Hibernate 事务; nested exception is org.hibernate.TransactionException: commit failed嵌套异常是 org.hibernate.TransactionException: 提交失败

The containing class is a 'Dao' class and is annotated as @Repository.包含类是一个“Dao”类,并被注释为@Repository。

First of all, is my migration of the code correct?首先,我的代码迁移是否正确? Second, what is causing the exception and how can I fix it?其次,是什么导致了异常,我该如何解决?

It might be because of the session is closed.可能是因为会话已关闭。 In methods with Spring managed transaction ( @Transactional ) ,Spring will handle session commit and session close.在 Spring 管理事务 ( @Transactional ) 的方法中,Spring 将处理会话提交和会话关闭。 If you do session.close() , the above error will appear.如果您执行session.close() ,则会出现上述错误。 comment session.close() and try it.评论session.close()并尝试一下。

@Transactional
public AcmeUserBean loadUserByDn(String userDn) {
    AcmeUserBean result = null;
    Session session = this.getSessionFactory().getCurrentSession();

        Query query = session.createQuery(
            "from AcmeUserBean as user where user.distinguishedName = :userDn");
        query.setString("userDn", userDn);
        List objs = query.list();

        if (objs != null && objs.size() > 0) {
            result = (AcmeUserBean) objs.get(0);
        }

        return result;
}

暂无
暂无

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

相关问题 迁移到Hibernate 4 + Spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程的事务同步Session - migration to hibernate 4 + spring 4.2.2: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread Spring + Hibernate应用程序中的问题:org.hibernate.HibernateException:找不到当前线程的Session - Problems in Spring + Hibernate application: org.hibernate.HibernateException: No Session found for current thread org.hibernate.HibernateException错误 - org.hibernate.HibernateException error JHipster Spring启动:org.hibernate.HibernateException:无法访问lob流 - JHipster Spring boot : org.hibernate.HibernateException: Unable to access lob stream Spring Data 2.1.1 org.hibernate.HibernateException:获得了不同大小的元组和别名? - Spring Data 2.1.1 org.hibernate.HibernateException: Got different size of tuples and aliases? 春季-&gt; org.hibernate.HibernateException:非法尝试将一个集合与两个打开的会话相关联 - spring -> org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions Spring Security authenticationFailure:错误org.hibernate.HibernateException:没有找到当前线程的会话 - Spring Security authenticationFailure : error org.hibernate.HibernateException: No Session found for current thread spring-data-jpa:找到对集合 org.hibernate.HibernateException 的共享引用 - spring-data-jpa : Found shared references to a collection org.hibernate.HibernateException 基本的休眠程序中的org.hibernate.HibernateException - org.hibernate.HibernateException in a basic hibernate program org.hibernate.HibernateException:无法实例化resultclass - org.hibernate.HibernateException: Could not instantiate resultclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM