Toda vez que reinicio o servido, os dados são apagados do banco. Alguém já passou por isso e pode dar uma ajuda? Estou usando Spring boot com JPA, hibernate, MySQL, WEB e Rest.
Um das minhas entidades:
package br.edu.unifacear.unilibrary.Unilibrary.model.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class Autor {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String nome;
private Integer quantEditais;
//construtor
//getters e setters
Repository:
package br.edu.unifacear.unilibrary.Unilibrary.model.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import br.edu.unifacear.unilibrary.Unilibrary.model.entity.Autor;
public interface AutorRepository extends JpaRepository<Autor, Integer>{
}
Controller:
package br.edu.unifacear.unilibrary.Unilibrary.controller;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import br.edu.unifacear.unilibrary.Unilibrary.model.entity.Autor;
import br.edu.unifacear.unilibrary.Unilibrary.model.repository.AutorRepository;
@RestController
public class AutorController {
private final AutorRepository autorRep;
AutorController(AutorRepository autorRep){
this.autorRep = autorRep;
}
@PostMapping("/unilibrary/autor/inserir")
public void inserir(@RequestBody Autor a) {
this.autorRep.save(a);
}
@GetMapping("/unilibrary/autor/listar")
public List<Autor> listar() {
return this.autorRep.findAll();
}
}
application.properties
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mysql://localhost:3306/unilibrary1?useTimezone=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=***
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=create
#JPA
spring.jpa.open-in-view=true
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.edu.unifacear.unilibrary</groupId>
<artifactId>Unilibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Unilibrary</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>