Java Springboot:Hibernate配置

Java Springboot:Hibernate配置,java,spring,hibernate,spring-boot,Java,Spring,Hibernate,Spring Boot,我添加了application.properties文件,如下所示: Spring数据源(DataSourceAutoConfiguration&DataSourceProperties)Spring.DATASOURCE.url= jdbc:mysql://localhost:3306/test?useSSL=false spring.datasource.username=根spring.datasource.password=根 休眠属性 SQL方言使Hibernate为所选数据库spri

我添加了application.properties文件,如下所示:

Spring数据源(DataSourceAutoConfiguration&DataSourceProperties)Spring.DATASOURCE.url= jdbc:mysql://localhost:3306/test?useSSL=false spring.datasource.username=根spring.datasource.password=根

休眠属性 SQL方言使Hibernate为所选数据库spring.jpa.properties.Hibernate.dialogue生成更好的SQL= org.hibernate.dialogue.mysql5innodbdialogue

Hibernate ddl auto(创建、创建、删除、验证、更新)spring.jpa.Hibernate.ddl-auto=create

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

对于dao层,类:

package com.repository;

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

@Repository
public class DaoClass
{
    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    @Bean
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
}
现在运行springboot应用程序时,我面临以下错误:

***************************应用程序无法启动


说明:

com.repository.DaoClass中的字段sessionFactory需要一个 键入找不到的“org.hibernate.SessionFactory”

行动:

考虑在中定义“org.hibernate.SessionFactory”类型的bean 您的配置

POM.xml


4.0.0
弹簧靴
第一程序
战争
0.0.1-快照
org.springframework.boot
spring启动程序父级
2.0.2.1发布
firstprogram Maven Webapp
http://maven.apache.org
org.springframework.boot
SpringBootStarterWeb
org.hibernate
休眠验证器
5.3.6.最终版本
org.springframework.boot
spring引导启动器数据jpa
第一程序

Spring boot不会自动配置
会话工厂
,而是配置
EntityManager工厂
,您可以直接使用该工厂或通过以下方式从中获取会话工厂:

SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);

添加上述解决方案时可能出现的重复,面临NullPointerException我以为我们正在使用
spring.jpa.properties.hibernate.current\u session\u context\u class=org.springframework.orm.hibernate4.SpringSessionContext
来配置sessionfactory