【发布时间】:2026-01-21 18:00:01
【问题描述】:
请在下面找到我的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 对象。
P.S:我知道我可以使用 ``spring-jpaandcrud 存储库` 来代替休眠。
事情是我必须使用休眠。将其视为一种限制。
错误:
说明: com.sagarp.employee.EmployeeDaoImpl 中的字段 sessionFactory 需要一个无法找到的“org.hibernate.SessionFactory”类型的 bean。
谁能帮帮我一下
【问题讨论】:
-
你试过this它需要application.properties中的属性和配置文件中的bean。
-
尝试 autowire EntityManager 并调用 unwrap 到 SessionFactory
-
你可以参考这个答案*.com/questions/45893879/…
-
没有
SessionFactory有一个EntityManagerFactory。您必须使用 Hibernate 的事实并不意味着您不能使用 JPA ... Hibernate 是 JPA 的(参考?)实现,所以从纯 JPA 开始,如果您真的需要纯 Hibernate API,请使用entitymanager.unwrap(Session.class)来获取底层会话。 -
@mallikarjun 已经尝试过了,但没有任何帮助。
标签: java spring hibernate spring-boot