bom dia galera, seguinte estou tentando inserir um registro usando Hibernate
eu tenho:
Telefone
Pessoa > Que tem um telefone
Usaurio > Que tem uma pessoa,
Usuario tem sua chave estrangeira com Pessoa
porem quando vou inserir o primeiro registro na tabela me lança a seguinte exeção:
11:25:47,125 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2224)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at br.inf.bios.entidade.TesteHibernate.main(TesteHibernate.java:58)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation, message from server: "Cannot add or update a child row: a foreign key constraint fails (`estudos1/usuario`, CONSTRAINT `usuario_ibfk_1` FOREIGN KEY (`pessoa_id`) REFERENCES `pessoa` (`id`))"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1461)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 15 more
Eu sei que é por causa da integridade do Banco não existe nenhum registro pra pessoa e tals, como eu resolvo isso, seria algo como.
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
Telefone telefone = new Telefone();
telefone.setDdd(062);
telefone.setFone(123456);
Pessoa pessoa = new Pessoa();
pessoa.setEmail("[email removido]");
pessoa.setPrimeironome("TESTE");
pessoa.setSegundonome("Teste");
pessoa.setTelefone(telefone);
Usuario usuario = new Usuario();
usuario.setPessoa(pessoa);
usuario.setSenha("123456");
usuario.setDtnascimento(new Date());
session.save(usuario);
help ai galera vlew…