3

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>

1 Resposta 1

5

O problema no parametro abaixo:

spring.jpa.hibernate.ddl-auto=create

Que define que toda a base será recriada toda vez que o Hibernate for iniciado

Mude para update para que seja criada apenas as novas tabelas que você definir com @Entity

spring.jpa.hibernate.ddl-auto=update
3
  • Obrigado pela ajuda, mas apagando essa linha, apresenta outro erro: 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 Commented 30/05/2019 às 18:35
  • fiz assim: spring.jpa.hibernate.ddl-auto= spring.jpa.hibernate.ddl-auto=update Compilou, não apagou os dados e criou uma nova tabela que fiz para teste. Obrigado pela ajuda Commented 30/05/2019 às 19:05
  • Tenta usar esse spring.jpa.hibernate.ddl-auto=none Commented 30/05/2019 às 19:06

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .