Update no Hibernate [RESOLVIDO]

3 respostas
N

Bom dia estou tendo algumas dificuldades em realizar um update no hibernate

é o seguinte:

tenho quatro 6 tabelas relacionadas

PessoaFisica - PK id

Aluno - FK - id_pessoa PK matricula
Responsavel - FK - id_pessoa
Professor - FK - id_pessoa
Endereco - FK - id_pessoa
Contatos - FK - id_pessoa

Quando vou inserir no banco de dados um novo aluno, insiro os dados na tabela pessoa e depois na tabela aluno até ai td bem roda bacana, mas quando vou atualizar um registro, aí acontece uma serie de erros...

código:

private void save(){
        Aluno aluno = new Aluno();
        AlunoId mat = new AlunoId();
        PessoaFisica pessoa = new PessoaFisica();
        // Pessoa Fisica
        pessoa.setNome(nome.getText());
        pessoa.setMae(mae.getText());
        pessoa.setPai(pai.getText());
        pessoa.setCpf(cpf.getText());
        // Aluno
        aluno.setCertidaoNascimento(certidaoNasc.getText());
        aluno.setCertidaoCartorio(certCartorio.getText());
        aluno.setCertidaoExpedicao(Formatter.format2Db(certExp.getText()));
        // Verificando os dados
        if (idAluno != null && idPessoa != null){
            pessoa.setId(idPessoa);
            mat.setMatricula(idAluno);
            aluno.setId(mat);
            aluno.setPessoaFisica(pessoa);
            alunoDAO.update(aluno);
            pessoaDAO.update(pessoa);
            System.out.println("Atualizado com sucesso!");
        }
        else {
            pessoaDAO.save(pessoa);
            mat.setMatricula(alunoDAO.getLastMatricula());
            mat.setIdPessoa(pessoa.getId());
            aluno.setId(mat);
            aluno.setPessoaFisica(pessoa);
            alunoDAO.save(aluno);
            System.out.println("salvo com sucesso!");
        }
        
    }

Quando tento atualizar o registro acontece o seguinte:

Hibernate: update public.aluno set certidao_nascimento=?, certidao_expedicao=?, certidao_cartorio=? where id_pessoa=? and matricula=?
14/01/2009 10:37:32 org.hibernate.jdbc.BatchingBatcher doExecuteBatch
SEVERE: Exception executing batch: 
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
        at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
        at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
        at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
        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 sigaclass.model.dao.hibernate.BaseHibernateDAO.update(BaseHibernateDAO.java:96)
        at sigaclass.view.AlunoView.save(AlunoView.java:182)
        at sigaclass.view.AlunoView.btSalvarActionPerformed(AlunoView.java:1219)
        at sigaclass.view.AlunoView.access$100(AlunoView.java:32)
        at sigaclass.view.AlunoView$2.actionPerformed(AlunoView.java:346)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6134)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5899)
        at java.awt.Container.processEvent(Container.java:2023)
        at java.awt.Component.dispatchEventImpl(Component.java:4501)
        at java.awt.Container.dispatchEventImpl(Container.java:2081)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
        at java.awt.Container.dispatchEventImpl(Container.java:2067)
        at java.awt.Window.dispatchEventImpl(Window.java:2458)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
14/01/2009 10:37:32 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
        at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
        at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
        at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
        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 sigaclass.model.dao.hibernate.BaseHibernateDAO.update(BaseHibernateDAO.java:96)
        at sigaclass.view.AlunoView.save(AlunoView.java:182)
        at sigaclass.view.AlunoView.btSalvarActionPerformed(AlunoView.java:1219)
        at sigaclass.view.AlunoView.access$100(AlunoView.java:32)
        at sigaclass.view.AlunoView$2.actionPerformed(AlunoView.java:346)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6134)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5899)
        at java.awt.Container.processEvent(Container.java:2023)
        at java.awt.Component.dispatchEventImpl(Component.java:4501)
        at java.awt.Container.dispatchEventImpl(Container.java:2081)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
        at java.awt.Container.dispatchEventImpl(Container.java:2067)
        at java.awt.Window.dispatchEventImpl(Window.java:2458)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
        at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
        at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
        at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
        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 sigaclass.model.dao.hibernate.BaseHibernateDAO.update(BaseHibernateDAO.java:96)
        at sigaclass.view.AlunoView.save(AlunoView.java:182)
        at sigaclass.view.AlunoView.btSalvarActionPerformed(AlunoView.java:1219)
        at sigaclass.view.AlunoView.access$100(AlunoView.java:32)
        at sigaclass.view.AlunoView$2.actionPerformed(AlunoView.java:346)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6134)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5899)
        at java.awt.Container.processEvent(Container.java:2023)
        at java.awt.Component.dispatchEventImpl(Component.java:4501)
        at java.awt.Container.dispatchEventImpl(Container.java:2081)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
        at java.awt.Container.dispatchEventImpl(Container.java:2067)
        at java.awt.Window.dispatchEventImpl(Window.java:2458)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Já tentei várias maneiras mas não consigo, alguém pode me dar uma luz?

3 Respostas

N

Galera não sei se foi a melhor prática, mas resolvi o problema criando o meu proprio update, não posso perder tempo… ficou + ou - assim:

@Override public void update(Aluno object){ String sql; sql = "update public.aluno set certidao_nascimento=?, certidao_expedicao=?, certidao_cartorio=? where id_pessoa=? and matricula=?"; Query update = getSession() .createSQLQuery(sql); update.setParameter(0, object.getCertidaoNascimento()); update.setParameter(1, object.getCertidaoExpedicao()); update.setParameter(2, object.getCertidaoCartorio()); update.setParameter(3, object.getId().getMatricula()); update.setParameter(4, object.getPessoaFisica().getId()); update.executeUpdate(); getSession().beginTransaction().commit(); }

Aí eu chamo AlunoDAO.update(aluno)
e funcionou bacana! resolveu o meu problema.

ignacio83

Se vc tiver que ficar fazendo todos os updates… tá ferrado…

O hibernate está dizendo que ele esperava alterar um registro porém nenhum registro foi alterado…

Já executou esse update na mão?

Que banco de dados é? Está com a versão mais nova do driver?

N

estou usando PostgreSQL 8.3, o driver é o mais novo, o meu update na mão deu certo,
em outras tabelas dá certo somente nessas relacionadas é que eu não consigo atualizar pelo hibernate, realmente eu percebo que ele não está encontrando a linha, mas quando tento passar o objeto idPessoa ele diz q já está em uso… talvez seja alguma coisa no mapeamento…mas por enquanto isso resolveu me problema.

Criado 14 de janeiro de 2009
Ultima resposta 15 de jan. de 2009
Respostas 3
Participantes 2