Spring Boot - initial test data

Asked

Viewed 230 times

0

I am trying to pass data to test the front, but the controller is not getting the data that Spring Boot should start.

My application.properties:

spring.jpa.hibernate.ddl-auto=create

My import.sql:

INSERT INTO convidado
    (id, nome, quantidadeDeAcompanhante)
VALUES(1, 'Pedro', 2);
INSERT INTO convidado
    (id, nome, quantidadeDeAcompanhante)
VALUES(2, 'Ana', 5);
INSERT INTO convidado
    (id, nome, quantidadeDeAcompanhante)
VALUES(3, 'Ricardo', 1);

My controller:

@Controller
public class ConvidadosController {
    @Autowired
    private Convidados convidados;
    @GetMapping("/convidados")
    public ModelAndView listar() {
        ModelAndView modelAndView = new ModelAndView("ListaConvidados");
        modelAndView.addObject("convidados", convidados.findAll());
        System.out.println("***** " + convidados.findAll().toString() + " *******");
        return modelAndView;
    }
}

My Invited Entity:

@Entity
public class Convidado implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id;
    private String nome;
    private Integer quantidadeDeAcompanhante;

My Pository:

import com.gestaofestas.model.Convidado;
public interface Convidados extends JpaRepository<Convidado, Long> {

}

My folders:

inserir a descrição da imagem aqui

In case it is returning an empty array and does not give error. How do I test this way by inserting data into the sql file that Spring Boot loads in Hibernate?

  • in fact the name that it will map to make this Insert is the "data.sql" no import.sql, check section 10.3 of the manual: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-database-initialization

1 answer

0


Solution:

  1. Put the following in your application.properties:
spring.datasource.initialization-mode=always
  1. Rename the file import.sql for data sql.

  2. Make sure the file data sql. is located in src/main/Resources

  3. Place @Column above the variables/columns name and quantityDepartnering

  4. If you have a script to create tables, the script has to be called schema.sql. Then put the following in your application.properties:

spring.jpa.hibernate.ddl-auto=none

See more information in the reference Database initialization spring.

  • I don’t have a schema.sql. It continues with getting nothing in the array. Looking at the print of the package hierarchy, do you think this could be it? Tá dando: Failed to execute SQL script statement #1 of URL [file:/C:/Users/Everton/Documents/gestao-festas/target/classes/data.sql]: INSERT INTO guest (id, name, quantityDecompanion) VALUES(1, 'Pedro', 2); nested Exception is org.H2.jdbc.Jdbcsqlsyntaxerrorexception: Column "QUANTIDADEDECOMPANION" not found; SQL statement: INSERT INTO guest (id, name, quantityDecompanion) VALUES(1, 'Pedro', 2) [42122-200]

  • @Evertonlevi JPA cannot find BD columns. Place @Column on top of variables/columns nome and quantidadeDeAcompanhante. If the error persists, create the schema.sql file and use CREATE TABLE... to create the structure of your table.

Browser other questions tagged

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