Unable to autowire sessionFactory in springboot












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










share|improve this question

























  • Have you tried this It need property in application.properties and bean in your configuration file.

    – mallikarjun
    Jul 19 '18 at 5:57













  • try autowire EntityManager and invoke unwrap to SessionFactory

    – Piotr R
    Jul 19 '18 at 5:58











  • you can take reference from this answer stackoverflow.com/questions/45893879/…

    – Ajit Soman
    Jul 19 '18 at 6: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 6:04











  • @mallikarjun have tried that, but it is of no help.

    – bibliophilsagar
    Jul 19 '18 at 6:40
















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










share|improve this question

























  • Have you tried this It need property in application.properties and bean in your configuration file.

    – mallikarjun
    Jul 19 '18 at 5:57













  • try autowire EntityManager and invoke unwrap to SessionFactory

    – Piotr R
    Jul 19 '18 at 5:58











  • you can take reference from this answer stackoverflow.com/questions/45893879/…

    – Ajit Soman
    Jul 19 '18 at 6: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 6:04











  • @mallikarjun have tried that, but it is of no help.

    – bibliophilsagar
    Jul 19 '18 at 6:40














0












0








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










share|improve this question
















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







java spring hibernate spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 19 '18 at 6:42







bibliophilsagar

















asked Jul 19 '18 at 5:53









bibliophilsagarbibliophilsagar

1,165824




1,165824













  • Have you tried this It need property in application.properties and bean in your configuration file.

    – mallikarjun
    Jul 19 '18 at 5:57













  • try autowire EntityManager and invoke unwrap to SessionFactory

    – Piotr R
    Jul 19 '18 at 5:58











  • you can take reference from this answer stackoverflow.com/questions/45893879/…

    – Ajit Soman
    Jul 19 '18 at 6: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 6:04











  • @mallikarjun have tried that, but it is of no help.

    – bibliophilsagar
    Jul 19 '18 at 6:40



















  • Have you tried this It need property in application.properties and bean in your configuration file.

    – mallikarjun
    Jul 19 '18 at 5:57













  • try autowire EntityManager and invoke unwrap to SessionFactory

    – Piotr R
    Jul 19 '18 at 5:58











  • you can take reference from this answer stackoverflow.com/questions/45893879/…

    – Ajit Soman
    Jul 19 '18 at 6: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 6:04











  • @mallikarjun have tried that, but it is of no help.

    – bibliophilsagar
    Jul 19 '18 at 6:40

















Have you tried this It need property in application.properties and bean in your configuration file.

– mallikarjun
Jul 19 '18 at 5:57







Have you tried this It need property in application.properties and bean in your configuration file.

– mallikarjun
Jul 19 '18 at 5:57















try autowire EntityManager and invoke unwrap to SessionFactory

– Piotr R
Jul 19 '18 at 5:58





try autowire EntityManager and invoke unwrap to SessionFactory

– Piotr R
Jul 19 '18 at 5:58













you can take reference from this answer stackoverflow.com/questions/45893879/…

– Ajit Soman
Jul 19 '18 at 6:00





you can take reference from this answer stackoverflow.com/questions/45893879/…

– Ajit Soman
Jul 19 '18 at 6:00




1




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 6:04





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 6:04













@mallikarjun have tried that, but it is of no help.

– bibliophilsagar
Jul 19 '18 at 6:40





@mallikarjun have tried that, but it is of no help.

– bibliophilsagar
Jul 19 '18 at 6:40












1 Answer
1






active

oldest

votes


















0














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;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51415266%2funable-to-autowire-sessionfactory-in-springboot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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;
    }





    share|improve this answer




























      0














      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;
      }





      share|improve this answer


























        0












        0








        0







        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;
        }





        share|improve this answer













        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;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 20:41









        Vikas GuptaVikas Gupta

        34




        34






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51415266%2funable-to-autowire-sessionfactory-in-springboot%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)