You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 1.5.4集成Hibernate 5.3.5.Final遇openSession方法缺失的解决办法

Fixing "org.hibernate.SessionFactory.openSession() not found" in Spring Boot 1.5.4 + Hibernate 5.3.5.Final

Hey there, let's work through this issue together. I’ve run into similar compatibility headaches when mixing Spring Boot 1.x with newer Hibernate versions, so here’s what you need to know and fix:

1. Understand the Root Causes

First, a quick reality check: openSession() does exist in Hibernate 5.3’s SessionFactory—you’re likely facing one of these issues:

  • A dependency conflict pulling in mismatched Hibernate classes
  • Incorrectly accessing the SessionFactory within Spring’s JPA context
  • Spring Boot 1.5.x’s limited out-of-the-box support for Hibernate 5.3 (it officially supports up to 5.2.x)

In Spring Boot’s JPA environment, manually calling openSession() is generally not best practice—it creates a session outside Spring’s transaction management, forcing you to handle closing it manually. Instead, use Spring’s managed sessions via EntityManager:

Step 1: Inject EntityManager and unwrap to Hibernate Session

import org.hibernate.Session;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;

@Service
public class YourService {

    @PersistenceContext
    private EntityManager entityManager;

    @Transactional // Spring manages the session lifecycle automatically
    public void performDatabaseOperation() {
        Session session = entityManager.unwrap(Session.class);
        // Use session for Hibernate-native operations here
        // No need to close the session—Spring handles it when the transaction ends
    }
}

Why this works:

  • The @Transactional annotation binds a session to the current thread, so you get a managed session that’s auto-closed.
  • This aligns with Spring Boot’s JPA integration patterns and avoids version compatibility headaches.

If you have a strict need for an unmanaged session, ensure you’re getting the correct Hibernate SessionFactory instance:

Step 1: Expose Hibernate SessionFactory as a Spring Bean

Add this configuration class to unwrap the JPA EntityManagerFactory into a Hibernate SessionFactory:

import org.hibernate.SessionFactory;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HibernateConfig {

    @Bean
    public SessionFactory sessionFactory(EntityManagerFactory entityManagerFactory) {
        return entityManagerFactory.unwrap(SessionFactory.class);
    }
}

Step 2: Inject and Use the SessionFactory

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class YourService {

    @Autowired
    private SessionFactory sessionFactory;

    public void unmanagedSessionOperation() {
        try (Session session = sessionFactory.openSession()) { // Use try-with-resources to auto-close
            // Perform operations with the unmanaged session
        }
    }
}

4. Resolve Dependency Compatibility Issues

Spring Boot 1.5.4 pulls in Hibernate 5.0.x by default. To use 5.3.5.Final, exclude the default Hibernate core dependency and explicitly declare the 5.3 version:

Maven Example:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.3.5.Final</version>
</dependency>

Gradle Example:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-jpa') {
        exclude group: 'org.hibernate', module: 'hibernate-core'
    }
    implementation 'org.hibernate:hibernate-core:5.3.5.Final'
}

Verify No Dependency Conflicts

Run these commands to check for conflicting Hibernate versions:

  • Maven: mvn dependency:tree
  • Gradle: ./gradlew dependencies
    Ensure there are no references to Hibernate 3.x or older 5.x versions in the output.

Final Notes

Sticking with Spring-managed sessions via EntityManager is the most maintainable approach—it keeps your code aligned with Spring Boot’s conventions and reduces the risk of resource leaks. Only use openSession() if you have a strict requirement for an unmanaged session.

内容的提问来源于stack exchange,提问作者surya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:08:01