“ xxxDAOImpl”中的现场会话需要找不到“ org.hibernate.SessionFactory”类型的Bean


问题内容

我收到以下错误(我相信是Dao层-但我可能读错了)。

我现在有一个Spring
Boot应用程序,可以创建一个数据库模式。表格正在正确创建,但是当我尝试添加Dao和DaoImpl文件时,它崩溃并显示以下错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field session in xx.dao.ParkingSpaceDaoImpl 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.

在我的DaoImpl文件中,我有:

@Repository 
public class xxDaoImpl implements xxDao {
    @Autowired
    private SessionFactory session;

这是我的POM.xml文件的外观:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xx.xx</groupId>
    <artifactId>xxx</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> 
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

有谁知道如何解决这个问题?请让我知道,谢谢。


问题答案:

因此,我找到了适合我的解决方案。如果您正在使用配置.xml文件,则Mathias可能是正确的。但是对于那些使用application.properties文件的用户,您需要将此行添加到配置类或主应用程序类中:

@Bean  
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){  
    return hemf.getSessionFactory();  
}

完成后,将此行添加到application.properties文件:

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

这个解决方案对我有用。这是我能够处理的其他参考:

http://www.ekiras.com/2016/02/how-to-use-configure-session-factory-bean-
springboot.html