'Cant get database entries to persist in mySQL, using spring boot, JPA repository, postman

I could do with some help please, as I'm going round in circles here and can't find a solution. I have created a very basic crud app, a to do list. I have it running in eclipse, and can use postman with the corresponding requests to add a task to the database. The task shows in postman when I "/getAllTasks", but the tasks do not enter the mySQL database. I cant see any glaring errors in the Spring console.

As I understand it, I should be using the prod properties to input data from postman to the mySQL database I have manually created in Workbench using the mySQL commands below, and I should switch to the test properties to use the temporarily populated h2 database which does not persist, but is used for testing purposes.

When I use the prod properties, and use postman to send a post request, the mySQL database is not being populated.

I do apologise if there is something very obvious that I am missing, I have been trying to get this to work for hours, but can't seem to fix the issue. Any help would be greatly appreciated.

Ive added all my code below. Thank you

Entity Class

package com.qa.soloProject.model;

import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class ToDoList {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    @Column(unique = true, nullable = false, length = 200)
    String task;

    @Column(nullable = false)
    int difficultyRating;

    @Column(nullable = false, length = 50)
    String deadline;

    @Column(nullable = false)
    boolean complete;

    @Column(nullable = false, length = 200)
    String review;

    public ToDoList() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public int hashCode() {
        return Objects.hash(complete, deadline, difficultyRating, id, review, task);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ToDoList other = (ToDoList) obj;
        return complete == other.complete && deadline == other.deadline && difficultyRating == other.difficultyRating
                && id == other.id && Objects.equals(review, other.review) && Objects.equals(task, other.task);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public int getDifficultyRating() {
        return difficultyRating;
    }

    public void setDifficultyRating(int difficultyRating) {
        this.difficultyRating = difficultyRating;
    }

    public String getDeadline() {
        return deadline;
    }

    public void setDeadline(String deadline) {
        this.deadline = deadline;
    }

    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
    }

    public String getReview() {
        return review;
    }

    public void setReview(String review) {
        this.review = review;
    }

    @Override
    public String toString() {
        return "ToDoList [id=" + id + ", task=" + task + ", difficultyRating=" + difficultyRating + ", deadline="
                + deadline + ", complete=" + complete + ", review=" + review + "]";
    }

    public ToDoList(String task, int difficultyRating, String deadline, boolean complete, String review) {
        super();
        this.task = task;
        this.difficultyRating = difficultyRating;
        this.deadline = deadline;
        this.complete = complete;
        this.review = review;
    }

    public ToDoList(int id, String task, int difficultyRating, String deadline, boolean complete, String review) {
        super();
        this.id = id;
        this.task = task;
        this.difficultyRating = difficultyRating;
        this.deadline = deadline;
        this.complete = complete;
        this.review = review;
    }
}

Services

package com.qa.soloProject.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qa.soloProject.model.ToDoList;
import com.qa.soloProject.repo.Repo;

@Service
public class ServicesDB {

    @Autowired
    private Repo repo;

    public ToDoList addTask(ToDoList addedTask) {
        return repo.save(addedTask);

    }

    public List<ToDoList> addMultipleTasks(List<ToDoList> addedMultipleTasks) {
        return repo.saveAll(addedMultipleTasks);
    }

    public List<ToDoList> getAllTasks() {
        return repo.findAll();
    }

    public ToDoList getTaskById(int id) {
        return repo.findById(id).get();
    }

    public boolean deleteTaskById(int id) {
        repo.deleteById(id);
        return true;
    }

    public boolean deleteAllTasks() {
        repo.deleteAll();
        return true;
    }

    public ToDoList updateTask(ToDoList taskToUpdate) {

        ToDoList existingTask = repo.getById(taskToUpdate.getId());

        existingTask.setTask(taskToUpdate.getTask());
        existingTask.setDifficultyRating(taskToUpdate.getDifficultyRating());
        existingTask.setDeadline(taskToUpdate.getDeadline());
        existingTask.setComplete(taskToUpdate.isComplete());
        existingTask.setReview(taskToUpdate.getReview());

        return repo.save(existingTask);
    }

}

Controller

package com.qa.soloProject.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.qa.soloProject.model.ToDoList;
import com.qa.soloProject.services.ServicesDB;

@RestController
public class Controller {

    @Autowired
    private ServicesDB service;

    @PostMapping("/addTask")
    public ToDoList addTask(@RequestBody ToDoList newTask) {

        return service.addTask(newTask);

    }

    @PostMapping("/addTasks")
    public List<ToDoList> addAllTasks(@RequestBody List<ToDoList> newTasks) {
        return service.addMultipleTasks(newTasks);

    }

    @GetMapping("/getAllTasks")
    public List<ToDoList> getAllTasks() {

        return service.getAllTasks();
    }

    @GetMapping("/getTasksById/{taskId}")
    public ToDoList TaskById(@PathVariable int taskId) {

        return service.getTaskById(taskId);

    }

    @DeleteMapping("/delete/{taskId}")
    public ResponseEntity<String> deleteTaskById(@PathVariable int taskId) {
        service.deleteTaskById(taskId);
        return new ResponseEntity<String>("Task: " + service.getTaskById(taskId) + " deleted", HttpStatus.GONE);
    }

    @DeleteMapping("/deleteAll")
    public ResponseEntity<String> deleteAllTasks() {
        service.deleteAllTasks();
        return new ResponseEntity<String>("All tasks have been deleted", HttpStatus.ACCEPTED);
    }

}

Repo interface

package com.qa.soloProject.repo;

import org.springframework.data.jpa.repository.JpaRepository;

import com.qa.soloProject.model.ToDoList;

public interface Repo extends JpaRepository<ToDoList, Integer> {

}

Prod properties file

spring.datasource.url = jdbc:mysql://localhost:3306/hello
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

Test properties file

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.jpa.show-sql=true

MySQL database and table creation in workbench

CREATE DATABASE hello;
USE hello;

CREATE TABLE ToDoList (
    id int NOT NULL auto_increment,
    task varchar(255) NOT NULL,
    difficultyRating int NOT NULL,
    complete boolean NOT NULL,
    deadline varchar(255) NOT NULL,
    review varchar(255) NOT NULL,
    PRIMARY KEY (id)
);

SELECT * FROM ToDoList;



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source