Error inserting data with Spring

Asked

Viewed 274 times

1

I am entering data that the user type in the console, I take these values with the class Scanner of Java and play in the Postgres database using Spring Boot. The problem is when I will insert a company appears the following error:

Hibernate: insert into empregado (id_empresa, nome) values (?, ?)
2019-05-05 17:04:53.668  WARN 588 --- [  restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23502
2019-05-05 17:04:53.668 ERROR 588 --- [  restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value in column "id_empresa" violates not-null constraint
  Detalhe: Failing row contains (13, lala, null).
FUDEU: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [id_empresa]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
2019-05-05 17:05:41.806  INFO 588 --- [       Thread-7] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2019-05-05 17:05:41.807  INFO 588 --- [       Thread-7] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-05-05 17:05:41.809  INFO 588 --- [       Thread-7] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2019-05-05 17:05:41.856  INFO 588 --- [       Thread-7] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

I DON’T KNOW HOW TO FIX IT, PLEASE SOMEONE HELP ME

Code

try {
      Empregado empregado = new Empregado();
      System.out.println("Insira o NOME do empregado");
      String nome = new Scanner(System.in).nextLine();
      if (!nome.equals("") && nome != null) {
      System.out.println("Insira a empresa que o (a) " + nome + " trabalha");
      String empresaEmp = new Scanner(System.in).nextLine();
      if (!empresaEmp.equals("") && empresaEmp != null) {
            empresa.setNome(empresaEmp);
            empregado.setNome(nome);
            empregado.setEmpresa(empresa);
            empRep.save(empregado);
            emprRep.save(empresa);
      } else {
            System.out.println("É obrigatório informar a empresa em que: " + nome + " trabalha");
      }
                }
    } catch (Exception e) {
            System.out.println("FUDEU: " + e.toString());
    }

1 answer

1


The problem is here oh

ERROR: null value in column "id_company" violates not-null Constraint

You have to provide this id to the company or use some kind of automatic generation in the bank. But I think the mistake is here oh, save first the company and then the employee. Understand that you cannot have an employee save to a company that does not yet exist?

        empresa.setNome(empresaEmp);
        empregado.setNome(nome);
        empregado.setEmpresa(empresa);
        // inverti a ordem aqui.
        emprRep.save(empresa);
        empRep.save(empregado);

Browser other questions tagged

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