0
votes

I have added application.properties file , given below :

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

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 = create

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

for dao layer, class :

package com.repository;

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

@Repository
public class DaoClass
{
    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    @Bean
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
}

now on running the springboot application, i am facing below error :

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


Description:

Field sessionFactory in com.repository.DaoClass 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.

POM.xml

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>springboot</groupId>
	<artifactId>firstprogram</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>
	<name>firstprogram Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>5.3.6.Final</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>			
		</dependency>

	</dependencies>
	<build>
		<finalName>firstprogram</finalName>
	</build>
</project>

i have also add enter image description here

package com.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

@Component
public class DaoClass
{
    @PersistenceContext
    private EntityManager entityManger;

    public EntityManager getEntityManger()
    {
        return entityManger;
    }

    public void setEntityManger(EntityManager entityManger)
    {
        this.entityManger = entityManger;
    }
}
1
on adding the above solution , facing nullpointerexception - sparsh610

1 Answers

0
votes

Spring boot doesn't auto configure SessionFactory, instead it configures the EntityManagerFactory which you can use directly or obtain a SessionFactory from it in the following way:

SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);