【发布时间】:2020-06-01 03:55:06
【问题描述】:
我收到此错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field sessionFactory in com.demo.dao.EmployeeDAO required a bean of type 'org.hibernate.SessionFactory' that could not be found.
Action: Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.
我的HibernateUtil 班级是:
@Configuration
public class HibernateUtil {
@Autowired
private EntityManagerFactory factory;
@Bean
public SessionFactory getSessionFactory() {
if(factory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("Factory is not a hibernate factory.");
}
return factory.unwrap(SessionFactory.class);
}
}
我的EmployeeDao 班级是:
@Repository
public class EmployeeDAO {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
public void save(Employee emp) {
Session session = null;
try {
session = sessionFactory.openSession();
System.out.println("Session got.");
Transaction tx = session.beginTransaction();
session.save(emp);
tx.commit();
}catch(HibernateException he) {
he.printStackTrace();
}
}
}
application.properties 文件,
spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/manissh
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
【问题讨论】:
-
请发布 .property 文件
-
这是你唯一的错误吗?您似乎正在创建 SessionFactory bean。但也许创建该 bean 时出错?就像未能创建 EntityManagerFactory 一样?
-
在 getSessionFactory() 中放一条日志语句或放一个断点,以调试模式启动应用程序。我很确定永远不会调用该方法。当配置类不在 Spring 的默认类路径扫描机制将要查找的位置时,可能会发生这种情况。
标签: java spring spring-boot spring-data-jpa