首页 > 解决方案 > 无法在 springboot 中自动装配 sessionFactory

问题描述

请在我的daoImpl班级下面找到。

import java.util.List;

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

@Repository
public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private SessionFactory sessionFactory;
    // all my setter  and other methods
}

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

我的 springBoot 应用程序代码:

package com.sagarp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EmployeeHibernateApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmployeeHibernateApplication.class, args);
    }

}

我的application.properties文件:

server.port=8080
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true   

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/sagarp?useSSL=false
spring.datasource.username = root
spring.datasource.password = password_123


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
#spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

场景:当我启动我的 springboot 应用程序时,它无法自动装配sessionFactory对象。

PS:我知道我可以使用“spring-jpa andcrud repository”而不是hibernate。事情是我必须使用休眠。将其视为一种限制。

错误:

说明:com.sagarp.employee.EmployeeDaoImpl 中的字段 sessionFactory 需要一个无法找到的“org.hibernate.SessionFactory”类型的 bean。

谁能在这里帮我一点忙

标签: javaspringhibernatespring-boot

解决方案


如果您想知道版本低于 5.2 的 Hibernate 中 SessionFactory 的代码

private static final SessionFactory sessionFactory;
 static {
     try {
         sessionFactory = new Configuration().configure().buildSessionFactory();
     } catch (Throwable ex) {
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);

     }

 }
 public static SessionFactory getSessionFactory() {
     return sessionFactory;
 }

推荐阅读