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());
}
It worked out here man, thank you very much
– Marcos Paulo S. Rezende