org.hibernate.HibernateException when migrating from Spring 3 to Spring 4

Paul Reiners :

I am migrating an application from Spring 3 to Spring 4. In particular, I am migrating it to

    <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:

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:

@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. However, an exception occurs when the method exits:

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: commit failed

The containing class is a 'Dao' class and is annotated as @Repository.

First of all, is my migration of the code correct? Second, what is causing the exception and how can I fix it?

RamPrakash :

It might be because of the session is closed. In methods with Spring managed transaction (@Transactional) ,Spring will handle session commit and session close. If you do session.close(), the above error will appear. comment session.close() and try it.

@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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Migrating Spring 3 to Spring 4 and upgrading to Hibernate 4

Migrating from Spring 3 to Spring 4 - org.springframework.scheduling.quartz.CronTriggerBean

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set Spring Boot SqlServer

spring-data-jpa : Found shared references to a collection org.hibernate.HibernateException

org.hibernate.HibernateException: Missing column: auth_password in Spring Boot application

spring -> org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

Spring Security authenticationFailure : error org.hibernate.HibernateException: No Session found for current thread

Migrating to Spring MVC 4

Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread

Spring boot 3 org.hibernate.query.SemanticException when updating an enum data type

Migrating from Weblogic to Spring/HikariCP

Hibernate 4 Multi-Tenancy and Spring 3 Hibernate

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Exception : org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set.

getting org.hibernate.HibernateException: No CurrentSessionContext configured! when trying to connect to heroku-postgresql Hibernate

Spring OpenSessionInViewFilter for Hibernate 4

AbstractMethodError thrown by org.hibernate.internal.SessionFactoryImpl.<init> when upgrading from Spring Boot 1.3 to 1.4 RC1

NoSuchMethodError: registerAutoProxyCreatorIfNecessary with Spring 4, Hibernate 4 and Maven 3

Spring 5 with hibernate 3

Spring 3.1 Hibernate 4 exception for Inheritance [cannot be cast to org.hibernate.mapping.RootClass]

org.hibernate.HibernateException: Missing table: all

org.hibernate.HibernateException: Could not instantiate resultclass

Getting org.hibernate.HibernateException: No CurrentSessionContext configured

org.hibernate.HibernateException: two open sessions

Migrating to Spring Boot 3 with ActiveMQ "Classic"

MappingException when migrating from Hibernate 3 / Grails 2.2.4 to Hiberate 5 / Grails 3.2.4

Java, Spring, Hibernate cannot find org.springframework.orm.hibernate3.LocalSessionFactoryBean