JPA Deletes database data each time it restarts

Asked

Viewed 378 times

3

Every time I restart the service, the data is deleted from the database. Has anyone gone through this and can you help? I am using Spring boot with JPA, Hibernate, Mysql, WEB and Rest.

One of my entities:

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>

1 answer

4


The problem in the parameter below:

spring.jpa.hibernate.ddl-auto=create

That defines that the entire base will be recreated every time Hibernate is started

Change to update to create only the new tables you define with @Entity

spring.jpa.hibernate.ddl-auto=update
  • Thank you for the help, but deleting this line presents another error: 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 javax.persistence.Persistenceexception: [Persistenceunit: default] Unable to build Hibernate Sessionfactory; nested Exception is java.lang.Illegalargumentexception: Unrecognized legacy hibernate.hbm2ddl.auto value : update

  • I did so: spring.jpa.Hibernate.ddl-auto= spring.jpa.Hibernate.ddl-auto=update Compiled, did not erase the data and created a new table I made for testing. Thanks for the help

  • Try using that spring.jpa.hibernate.ddl-auto=none

Browser other questions tagged

You are not signed in. Login or sign up in order to post.