SpringBoot配置纯Hibernate ORM(非JPA)的方法咨询
Hey there! Great question—since you're already using spring-boot-starter-data-jpa, you’re actually already running Hibernate under the hood (it’s the default JPA provider for Spring Boot). The key here is getting direct access to the Hibernate Session so you can use those Hibernate-specific methods like session.createNativeQuery(...). Let me break down exactly how to do this:
Step 1: Confirm Your Dependencies Are Correct
You don’t need to replace the spring-boot-starter-data-jpa dependency—it already includes all the necessary Hibernate libraries. If you want to pin a specific Hibernate version (optional), you can add a direct hibernate-core dependency to override the default version from Spring Boot:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Optional: Specify a custom Hibernate version --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>6.4.4.Final</version> <!-- Use your desired version --> </dependency>
Step 2: Get Direct Access to the Hibernate Session
There are two common ways to get the Session object in your Spring Boot application:
Option 1: Unwrap from EntityManager
Inject the JPA EntityManager into your repository/service, then unwrap it to get the underlying Hibernate Session:
import org.hibernate.Session; import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; @Repository public class LectureScheduleRepository { @PersistenceContext private EntityManager entityManager; public List<LectureSchedule> fetchUsingNativeHibernateQuery(String query) { // Unwrap the EntityManager to get the Hibernate Session Session session = entityManager.unwrap(Session.class); // Now you can use all native Hibernate methods! return session.createNativeQuery(query) .addEntity(LectureSchedule.class) .list(); } }
Option 2: Extend Spring Data JPA Repository
If you’re using Spring Data JPA repositories, you can add a default method to your repository interface that accesses the Session:
import org.hibernate.Session; import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation; import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import java.util.List; @Repository public interface LectureScheduleRepository extends JpaRepositoryImplementation<LectureSchedule, Long> { default List<LectureSchedule> findWithNativeHibernateQuery(String query) { EntityManager em = getEntityManager(); Session session = em.unwrap(Session.class); return session.createNativeQuery(query) .addEntity(LectureSchedule.class) .list(); } }
Step 3: Optional Hibernate Customizations
If you need to tweak Hibernate-specific settings, you can add them to your application.properties or application.yml:
# Enable SQL logging for debugging spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true # Set a custom Hibernate dialect (match your database) spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect # Other Hibernate properties can be added here too
A quick note: spring-boot-starter-data-jpa doesn’t limit your access to Hibernate’s features—it just abstracts them behind the JPA API. Unwrapping to the Session lets you use every native Hibernate method you need, no extra setup required.
内容的提问来源于stack exchange,提问作者Koshan Samarasinghe

