1

I am getting this error:

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

Description:

Field sessionFactory in com.demo.dao.EmployeeDAO 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.

My HibernateUtil class is:

@Configuration
public class HibernateUtil {

    @Autowired
    private EntityManagerFactory factory;

    @Bean
    public SessionFactory getSessionFactory() {
        if(factory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("Factory is not a hibernate factory.");
        }
        return factory.unwrap(SessionFactory.class);
    }
}

my EmployeeDao class is:

@Repository
public class EmployeeDAO {

    @Autowired
    private SessionFactory sessionFactory;
    
    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
    
    public void save(Employee emp) {
        
        Session session = null;
        
        try {
            session = sessionFactory.openSession();
            System.out.println("Session got.");
            Transaction tx = session.beginTransaction();
            session.save(emp);
            tx.commit();
        }catch(HibernateException he) {
            he.printStackTrace();
        }
    }
}

application.properties file,

spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/manissh
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

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

Community
  • 1
  • 1
  • pls post .property file – Ganesh Gudghe Feb 17 '20 at 08:51
  • Is that the only error you had ? You seem to be creating the SessionFactory bean. But maybe there's an error creating that bean ? Like failing to create EntityManagerFactory ? – Ali Ben Zarrouk Feb 17 '20 at 08:52
  • Put a log statement in getSessionFactory() or put a breakpoint and start the application in debug mode. I'm pretty sure that method will never be invoked. This can happen when the configuration class is not in a place where the default classpath scanning mechanism of Spring is going to look. – Gimby Feb 17 '20 at 08:57

2 Answers2

1

try to create Autowired sessionFactory in your Repository

   @Repository
   public class EmployeeDAO {
            private SessionFactory sessionFactory;

            @Autowired
            public EmployeeDAO(EntityManagerFactory entityManagerFactory) {
                    this.sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
            }
    }

or try this solution - https://stackoverflow.com/a/43895827/6582610

Meet Patel
  • 482
  • 4
  • 12
  • i want to handle the 'save' method in EmployeeDAO only. And want to keep EntityManagerFactory in HibernateUtil class only. So that i can use "session factory' in various classes. – Manissh Khaparde Feb 17 '20 at 09:05
  • why does this solution causes a cyclic dependency? it creates an entitymanager but autowired cases a circular dependency error :S – Numan Karaaslan Apr 29 '20 at 22:40
-1

You need not create session. Just add the dependency spring-boot-starter-jpa in pom.xml to use hibernate. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

If you do so , then you can directly give the query in repository.