Field session in 'xxxDAOImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found

11,229

So I found a solution that works for me. Mathias was probably right if you are working with a configuration .xml file. But for those who are using an application.properties file, you need to add this line to your configuration class or main application class:

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

Once done, add this line to the application.properties file:

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

This solution worked for me. Here are additional references that I was able to work off of:

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

Spring Boot - Handle to Hibernate SessionFactory

Share:
11,229
rj2700
Author by

rj2700

Take sum salt wit it.

Updated on June 14, 2022

Comments

  • rj2700
    rj2700 almost 2 years

    I am getting the following error (from I believe, the Dao layer - but I could be reading this wrong).

    I have a Spring boot app that right now, creates a DB schema. The tables are being create properly but when I tried adding the Dao and DaoImpl files, it crashes with the error message below:

    ***************************
    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.
    

    In my DaoImpl file, I have:

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

    Here's how my POM.xml file looks:

    <?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>
    

    Does anyone have any idea as to how I fix this? Please let me know, thanks.