I've made a CRUD REST API with Spring Boot, Spring Data JPA and MySQL and everything's okay. I'm using postman aswell, and everything seems okay, it is connected to my sqldb, it creates in my sql, updates, retrieve and deletes, but whenever I stop it, and start it again, all my data once created is gone. I need a solution for persisting the API's data.
My application.yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/cloud_vendor?useSSL=false
username: root
password: root
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
# The SQL dialect makes Hibernate generate better SQL for the chosen database
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
#JPA Settings
jpa.hibernate.ddl_auto: create
My CloudVendorController.java
package com.thinkcon.demo.controller;
import com.thinkcon.demo.model.CloudVendor;
import com.thinkcon.demo.service.CloudVendorService;
import java.util.List;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/cloudvendor")
public class CloudVendorController {
CloudVendorService cloudVendorService;
public CloudVendorController(CloudVendorService cloudVendorService)
{
this.cloudVendorService = cloudVendorService;
}
@GetMapping("{vendorId}")
public CloudVendor getCloudVendorDetails(@PathVariable("vendorId") String vendorId){
return cloudVendorService.getCloudVendor(vendorId);
}
@GetMapping()
public List<CloudVendor> getAllCloudVendorDetails(){
return cloudVendorService.getAllCloudVendors();
}
@PostMapping
public String createCloudVendorDetails(@RequestBody CloudVendor cloudVendor)
{
cloudVendorService.createCloudVendor(cloudVendor);
return "CloudVendor Created Successfully";
}
@PutMapping
public String updateCloudVendorDetails(@RequestBody CloudVendor cloudVendor)
{
cloudVendorService.updateCloudVendor(cloudVendor);
return "CloudVendor updated Successfully";
}
@DeleteMapping("{vendorId}")
public String deleteCloudVendorDetails(@PathVariable("vendorId")String vendorId)
{
cloudVendorService.deleteCloudVendor(vendorId);
return "CloudVendor deleted Successfully";
}
}
And my model\CloudVendor.java
package com.thinkcon.demo.model;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name="cloud_vendor_info")
public class CloudVendor
{
@Id
private String vendorId;
private String vendorName;
private String vendorAddress;
private String vendorPhoneNumber;
public CloudVendor(String vendorId, String vendorName, String vendorAddress, String vendorPhoneNumber) {
this.vendorId = vendorId;
this.vendorName = vendorName;
this.vendorAddress = vendorAddress;
this.vendorPhoneNumber = vendorPhoneNumber;
}
public CloudVendor() {
}
public String getVendorId() {
return vendorId;
}
public String getVendorName() {
return vendorName;
}
public String getVendorAddress() {
return vendorAddress;
}
public String getVendorPhoneNumber() {
return vendorPhoneNumber;
}
public void setVendorId(String vendorId) {
this.vendorId = vendorId;
}
public void setVendorName(String vendorName) {
this.vendorName = vendorName;
}
public void setVendorAddress(String vendorAddress) {
this.vendorAddress = vendorAddress;
}
public void setVendorPhoneNumber(String vendorPhoneNumber) {
this.vendorPhoneNumber = vendorPhoneNumber;
}
}
I need the solution, I can't have my data being restarted all over again everytime I just end it.
There are 5 types ;
create -> drops existing tables, then creates new tables.
update -> creates, compares with old ones, if there are changes, then updates and never deletes the existing tables or columns.
create-drop -> similar to create, but it will drop the database after shutdown.
validate -> validates whether the tables and columns exist, otherwise it throws an exception for that.
none -> turns off the DDL generation.
So, you will need ;
jpa.hibernate.ddl_auto: update