2

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.

rj2700
  • 1,770
  • 6
  • 28
  • 55

3 Answers3

7

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

Community
  • 1
  • 1
rj2700
  • 1,770
  • 6
  • 28
  • 55
  • Is there a way to upvote you more than once? I have been looking for xml free configuration for a while now, this is the only one that worked. – Poly Nov 28 '16 at 15:18
  • Additionally, in my case, I had to use the `@Transactional` annotation on the method where I was using the `SessionFactory`. :) Thank you. – codepleb Feb 02 '17 at 08:30
  • 1
    Where does HibernateEntityManagerFactory come from? – yglodt May 17 '17 at 19:32
0

First of all ensure the Hibernate dependency is on your classpath.

If it is, you should define your SessionFactory in your Spring applicationContext.

In the case you are using Hibernate4, the configuration must be something like this:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        </props>
    </property>

    <property name="packagesToScan">
        <list>
            <value>com.gya.model</value>
        </list>
    </property>
</bean>

This definition allows you to connect to your DB and Spring will cause an exception if no valid SessionFactory is found.

Mathias G.
  • 4,875
  • 3
  • 39
  • 60
  • What if I don't use hibernate.cfg.xml files and I only use annotations? I'm relatively new to Spring/Hibernate and I'm using the Spring STS IDE to create a starter project. – rj2700 Nov 16 '16 at 16:17
0

I am having a spring boot project for which I want to add session factory to manage session. Similar problem for which my workaround is as follows

  1. Remove @Autowired from SessionFactory
  2. Add entityManger
  3. Annotate it with @PersistenceContext

Like below

@PersistenceContext
private EntityManager entityManger;

private  SessionFactory sessionFactory;