0

Please find below my daoImpl class.

import java.util.List;

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

@Repository
public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private SessionFactory sessionFactory;
    // all my setter  and other methods
}

pom.xml :

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

My springBoot app code:

package com.sagarp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EmployeeHibernateApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmployeeHibernateApplication.class, args);
    }

}

My application.properties file:

server.port=8080
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true   

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/sagarp?useSSL=false
spring.datasource.username = root
spring.datasource.password = password_123


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
#spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

Scenario: When I start my springboot app, it is unable to autowire the sessionFactory object.

P.S: I am aware that I can use the ``spring-jpaandcrud repository` instead of hibernate. Thing is I have to use hibernate. Think of it as a restriction.

Error:

Description: Field sessionFactory in com.sagarp.employee.EmployeeDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.

Could anyone help me a bit here

bibliophilsagar
  • 1,713
  • 1
  • 19
  • 40
  • Have you tried [this](https://stackoverflow.com/questions/43895643/how-to-autowire-hibernate-sessionfactory-in-spring-boot) It need property in application.properties and bean in your configuration file. – mallikarjun Jul 19 '18 at 05:57
  • try autowire EntityManager and invoke unwrap to SessionFactory – Piotr Rogowski Jul 19 '18 at 05:58
  • you can take reference from this answer https://stackoverflow.com/questions/45893879/spring-boot-thymeleaf-hibernate-sessionfactory-bean-with-java-annotations/45894600#45894600 – Ajit Soman Jul 19 '18 at 06:00
  • 1
    There is no `SessionFactory` there is an `EntityManagerFactory`. The fact that you have to use Hibernate doesn't mean you cannot use JPA... Hibernate is an (the reference?) implementation for JPA, so start with plain JPA and if you really need the plain hibernate API use `entitymanager.unwrap(Session.class)` to get the underlying session. – M. Deinum Jul 19 '18 at 06:04
  • @mallikarjun have tried that, but it is of no help. – bibliophilsagar Jul 19 '18 at 06:40
  • Are you aware that Hibernate *is* JPA? (As of the latest versions, `SessionFactory extends EntityManagerFactory`.) – chrylis -cautiouslyoptimistic- Jul 19 '18 at 06:43
  • @chrylis that is only the case for Hibernate 5.2 and later, prior to that that wasn't the case. – M. Deinum Jul 19 '18 at 08:11
  • @M.Deinum Thus "latest versions". – chrylis -cautiouslyoptimistic- Jul 19 '18 at 17:58
  • Although in the latest versions that is the case that also lead to issues on Spring 5.0 trying to combine Hibernate and JPA (transactionamangement for instance). Fixes are being made for Spring 5.1. – M. Deinum Jul 20 '18 at 05:54
  • @M.Deinum thanks for the help. I used `entityManagerFactory` – bibliophilsagar Jul 20 '18 at 09:26

1 Answers1

-1

In case if you wanted to know the code for SessionFactory in Hibernate with version lesser than 5.2

private static final SessionFactory sessionFactory;
 static {
     try {
         sessionFactory = new Configuration().configure().buildSessionFactory();
     } catch (Throwable ex) {
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);

     }

 }
 public static SessionFactory getSessionFactory() {
     return sessionFactory;
 }
Vikas
  • 23
  • 1
  • 8