I am getting following error whenever started application spring boot.
APPLICATION FAILED TO START
Description:
Field session in com.base.model.AbstractDao 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.
I have added implementation of my application:
POM.xml
<?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>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </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-web</artifactId> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Hibernate dependency --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.0.3.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> application.property
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root hibernate.dialect = org.hibernate.dialect.MySQLDialect spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext Configuration class
@Configuration @EnableTransactionManagement @ComponentScan({"configure"}) @PropertySource({"classpath:application.properties"}) public class HibernateConfiguration { @Autowired private Environment environment; @Bean public LocalSessionFactoryBean sessionFactory(){ LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan(new String[]{"com.base","com.base.model"}); sessionFactory.setMappingResources(new String[]{"Employee.hbm.xml"}); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory; } @Bean public Properties hibernateProperties() { Properties properties = new Properties(); properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); properties.put("hibernate.show_sql", environment.getRequiredProperty("hiberante.show_sql")); properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql")); return properties; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); dataSource.setUsername(environment.getRequiredProperty("jdbc.userName")); dataSource.setUsername(environment.getRequiredProperty("jdbc.password")); return dataSource; } @Bean public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){ return hemf.getSessionFactory(); } } Employee.java
public class Employee implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private String country; public int getId(){ return this.id; } public void setId(int id){ this.id = id; } public String getName(){ return this.name; } public void setName(String name){ this.name = name; } public void setCountry(String country){ this.country = country; } public String getCountry(){ return this.getCountry(); } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", country=" + country + "]"; } } Employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.base.model.Employee" table="Person"> <id name="id" type="java.lang.Integer"> <generator class="native"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" not-null="true"></column> </property> <property name="country" type="java.lang.String"> <column name="country"></column> </property> </class> </hibernate-mapping> EmployeeDaoImpl
@Component public class EmployeeDataDaoImpl { @Autowired SessionFactory sessionFactory; public List<Employee> findAllEmployee(){ //// Criteria cri = getSession().createCriteria(Employee.class); // List<Employee> dbList = cri.list(); // for (Employee employee : dbList) { // System.out.println(employee.getCountry()); // } return null; } } I have looked up the same error code on stackoverflow but none of the solutions worked and thus posting it here again with my code. Hoping someone else can point out where I am going wrong.