Ajuda com metodo atualizar no hibernate

3 respostas
anapaulasys

[b]Olá pessoal, sou iniciante com hibernate, tenho um cadastro de cidade que está incluindo, porem não estou conseguindo fazer a atualização. será que alguém pode me ajudar??

seguinte codigo.

classe Tela Cidade.

if(e.getSource() == btAlterar){

Cidade cidade=new Cidade();
           	cidade.setNome(nome_cid.getText());
        	cidade.setCodestado(uf_cid.getValor());
        	CidadeDao cidDao = new CidadeDao();
        	cidDao.update(cidade);
        	cod_cid.setText(Long.toString(cidade.getCodigo()));
        	 habilitaBotoes(false);
        	jlMensagem.setText("Alterado");
            statusTela = ALTERANDO;
           
          
        }
Na classe CidadeDao

public void update(Cidade cidade ){

this.session.beginTransaction();

this.session.update(cidade);

this.session.getTransaction().commit();

}

o erro que aparece é>>

to open PreparedStatement (open PreparedStatements: 0, globally: 0)

10:12:05,093 DEBUG SQL:111 -

update

public.Cidade

set

nome_cid=?,

uf_cid=?

where

cod_cid=?

10:12:05,097 DEBUG AbstractBatcher:66 - Executing batch size: 1

10:12:05,108 ERROR AbstractBatcher:73 - 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:85)

at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)

at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)

at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)

at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)

at Persistencia.dao.CidadeDao.update(CidadeDao.java:26)

at Telas.TelaCidade.actionPerformed(TelaCidade.java:55)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

10:12:05,112 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)

10:12:05,202 ERROR AbstractFlushingEventListener:324 - 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:85)

at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)

at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)

at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)

at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)

at Persistencia.dao.CidadeDao.update(CidadeDao.java:26)

at Telas.TelaCidade.actionPerformed(TelaCidade.java:55)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Exception in thread "AWT-EventQueue-0" 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:85)

at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)

at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)

at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)

at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)

at Persistencia.dao.CidadeDao.update(CidadeDao.java:26)

at Telas.TelaCidade.actionPerformed(TelaCidade.java:55)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Aguardo ajuda pessoal…
obrigada. :!:

3 Respostas

D

Já tive problema parecido, verifique se o cod_cid da tabela está sendo preenchido corretamente…
Se estiver, sai debugando pra ver os dados que estão indo no update…
Verifica se são correspondentes…

Se nada resolver, da uma procurada aqui no fórum que já vi esse erro por mal uso de sessão…

[]s

ngduran

Para que você consiga alterar um registro na tabela do banco de dados é necessário informar o id da chave primária do registro que se quer alterar, logo você deve passar um objeto completo incuindo o código do mesmo.

Você está setando os atributos e não está setando o código, logo o Hibernate não sabe que registro alterar e lança uma exceção.

Primeiro faça um método de busca onde retorne um objeto para a sua aplicação, onde possa ser alterado os dados, em seguida faça a chamada do método alterar.

Magno_LC

é bom lembrar q vc deveria por seu código na tag

[code]

fica melhor para visualização e organização

é um dos botões que aparece logo abaixo do campo 'Assunto'

Criado 21 de fevereiro de 2010
Ultima resposta 21 de fev. de 2010
Respostas 3
Participantes 4