0

When I start my springboot application I am getting an error mentioning

field materialRequestRepo in com.sql.csse.ControllerManager.MaterialRequestController required a bean named 'entityManagerFactory' that could not be found.

and I searched for answer and I found this one

As you can see in that question's answer he asked to remove

<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-core</artifactId>
 <version>4.1.4.Final</version>
</dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>5.2.3.Final</version>
</dependency>

But in my case when I remove those dependencies I am getting an error mentioning that,

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

And for that problem as you can see here they asked to add the same dependencies that I removed before.

What may be the problem occurred here?

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.sql.test</groupId>
  <artifactId>SpringBoot-sqlTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringBoot-sqlTest</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.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>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

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

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

    <dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.1</version>
    </dependency>

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.3.11.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.2.3.Final</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>


</project>

MaterialRequestController

package com.sql.csse.ControllerManager;


import com.google.gson.Gson;
import com.sql.csse.EntityManager.MaterialRequest;
import com.sql.csse.EntityManager.Order;
import com.sql.csse.EntityManager.Supplier;
import com.sql.csse.RepositoryManager.MaterialRequestRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Requests")
public class MaterialRequestController {

    @Autowired
    MaterialRequestRepo materialRequestRepo;

    MaterialRequest materialRequest;
    List<MaterialRequest> materialList;

    @RequestMapping(method = RequestMethod.POST , value = "/save" , produces = MediaType.APPLICATION_JSON_VALUE)
    public List<MaterialRequest> MaterialRequests(@RequestBody  String mr){

        Gson gson = new Gson();
        materialRequest =  gson.fromJson(mr, MaterialRequest.class);
        materialRequestRepo.save(materialRequest);
        return materialRequestRepo.findAll();
    }

    @RequestMapping(method = RequestMethod.GET , value = "/getall" , produces = MediaType.APPLICATION_JSON_VALUE)
    public List<MaterialRequest> getAll(){
        return materialRequestRepo.findAll();
    }

    @RequestMapping(method = RequestMethod.GET , value = "/getallPending" , produces = MediaType.APPLICATION_JSON_VALUE)
    public ArrayList<MaterialRequest> getPendingRequests(){

        return materialRequestRepo.findpendingRequests();


    }

}

MaterialRequest(Entity)

package com.sql.csse.EntityManager;


import javax.persistence.*;

@Entity

@Table(name ="material_requests")
public class MaterialRequest {

    @Id
    @Column(name = "RID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int RID;


    @Column(name = "MID")
    private int MID;

    @Column(name = "material_name")
    private String material_name;

    @Column(name = "material_quantity")
    private double material_quantity;

    @Column(name = "requested_date")
    private String requested_date;

    @Column(name = "order_date")
    private String order_date;

    @Column(name = "satatus")
    private String status;


    public MaterialRequest(int MID, String material_name, double material_quantity, String requested_date, String order_date, String status) {
        this.MID = MID;
        this.material_name = material_name;
        this.material_quantity = material_quantity;
        this.requested_date = requested_date;
        this.order_date = order_date;
        this.status = status;

    }

    public MaterialRequest() {
    }

    public int getRID() {
        return RID;
    }

    public void setRID(int RID) {
        this.RID = RID;
    }

    public int getMID() {
        return MID;
    }

    public void setMID(int MID) {
        this.MID = MID;
    }

    public String getMaterial_name() {
        return material_name;
    }

    public void setMaterial_name(String material_name) {
        this.material_name = material_name;
    }

    public double getMaterial_quantity() {
        return material_quantity;
    }

    public void setMaterial_quantity(double material_quantity) {
        this.material_quantity = material_quantity;
    }

    public String getRequested_date() {
        return requested_date;
    }

    public void setRequested_date(String requested_date) {
        this.requested_date = requested_date;
    }

    public String getOrder_date() {
        return order_date;
    }

    public void setOrder_date(String order_date) {
        this.order_date = order_date;
    }

    public String getStatus() { return status; }

    public void setStatus(String status) { this.status = status; }
}
Ramesh
  • 2,297
  • 2
  • 20
  • 42
  • For starters stop mixing Spring Boot Versions (2.0.1 and 2.0.5) and hibernate versions (4.3.11 and 5.2.3). Remove the hibernate dependencies as those are managed already by `spring-boot-starter-data-jpa`. – M. Deinum Oct 01 '18 at 18:46
  • @M.Deinum As I mentioned in the question when I remove those dependencies I am getting the `entityManager` error – Ramesh Oct 02 '18 at 02:26
  • 1
    And as stated you are mixing Spring Boot jars from different versions, don't. The main issue you have is the fact that your dependencies are a mess. Mixing different jars from a framework (Spring Boot and Hibernate) is trouble (like this) waiting to happen. So fix the dependencies do a `mvn dependency:purge-local-repository` to clear the used dependencies so that you get a fresh copy (maybe you have a broken jar in there). – M. Deinum Oct 02 '18 at 05:35
  • @M.Deinum I am really new to SpringBoot, that might cause this noob issue. I will learn about the dependency management. Thank you for your time sir. – Ramesh Oct 02 '18 at 11:07

2 Answers2

0

I have replicated your code and with this pom.xml all works fine:

 <project>
    <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>2.0.5.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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

this is my application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/dto
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
Federico Gatti
  • 535
  • 1
  • 9
  • 22
0

I solved it after many hours: i changed the version of spring-boot-starter-parent to the latest 2.2.3.RELEASE

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/>
    <!-- lookup parent from repository -->
</parent>