Problems running the DDL on a Rest Api via JPA

Asked

Viewed 35 times

-2

When I go to execute I realize this mistake:

org.hibernate.tool.schema.spi.Commandacceptanceexception: Error executing DDL "create table tb-product (id int8 not null, name varchar(255), quantity Numeric(19, 2), value Numeric(19, 2), Primary key (id)" via JDBC Statement

Main Class

package com.produtos.apirest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@EntityScan("com.produtos.apirest.models")

@SpringBootApplication
public class ApirestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApirestApplication.class, args);
    }

}

Model

package com.produtos.apirest.models;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="TB-PRODUTO")
public class Produto implements Serializable{

    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;
    
    private String nome;
    
    private BigDecimal quantidade;
    
    private BigDecimal valor;

    public long getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public BigDecimal getQuantidade() {
        return quantidade;
    }

    public void setQuantidade(BigDecimal quantidade) {
        this.quantidade = quantidade;
    }

    public BigDecimal getValor() {
        return valor;
    }

    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }
    
    
    

}

Aplication properties

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true


#Banco local - Luiz
spring.datasource.url= jdbc:postgresql://localhost:5432/produtos-apirest
spring.datasource.username=luiz
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update



2 answers

0

Identifiers in the database cannot contain invalid characters, as in this case '-', i.e., the name of your "TB-PRODUCT" table is invalid. The correct name should be "TB_PRODUTO".

-1

If your Apirestapplication class is at the root of the project you do not need to have this @Entityscan("com.productos.apirest.models") setting 'Cause Spring will read everything as long as it’s at the root.

Regarding the error you are taking may be the property below: spring.jpa.Hibernate.ddl-auto=update

Change to:

spring.jpa.Hibernate.ddl-auto=create

more information: https://docs.spring.io/spring-boot/docs/1.1.0.M1/reference/html/howto-database-initialization.html

https://stackoverflow.com/questions/42135114/how-does-spring-jpa-hibernate-ddl-auto-property-exactly-work-in-spring

Browser other questions tagged

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