Erro ao atualizar, Hibernate

4 respostas
A

Olá meus caros, estou desenvolvendo um sistema em java ultilizando hibernate para mapeamento do banco.
Todos os cadastros e exclusões estão funcionando, porém ao tentar atualizar dá o seguinte erro:

31/07/2010 19:45:56 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 persistencia.ServicoDao.atualizar(ServicoDao.java:159)

at windows.alterar.AlterarServico.salvarActionPerformed(AlterarServico.java:315)

at windows.alterar.AlterarServico.access$400(AlterarServico.java:35)

at windows.alterar.AlterarServico$3.actionPerformed(AlterarServico.java:217)

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:6263)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

at java.awt.Component.processEvent(Component.java:6028)

at java.awt.Container.processEvent(Container.java:2041)

at java.awt.Component.dispatchEventImpl(Component.java:4630)

at java.awt.Container.dispatchEventImpl(Container.java:2099)

at java.awt.Component.dispatchEvent(Component.java:4460)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)

at java.awt.Container.dispatchEventImpl(Container.java:2085)

at java.awt.Window.dispatchEventImpl(Window.java:2478)

at java.awt.Component.dispatchEvent(Component.java:4460)

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)

31/07/2010 19:45:56 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 persistencia.ServicoDao.atualizar(ServicoDao.java:159)

at windows.alterar.AlterarServico.salvarActionPerformed(AlterarServico.java:315)

at windows.alterar.AlterarServico.access$400(AlterarServico.java:35)

at windows.alterar.AlterarServico$3.actionPerformed(AlterarServico.java:217)

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:6263)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

at java.awt.Component.processEvent(Component.java:6028)

at java.awt.Container.processEvent(Container.java:2041)

at java.awt.Component.dispatchEventImpl(Component.java:4630)

at java.awt.Container.dispatchEventImpl(Container.java:2099)

at java.awt.Component.dispatchEvent(Component.java:4460)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)

at java.awt.Container.dispatchEventImpl(Container.java:2085)

at java.awt.Window.dispatchEventImpl(Window.java:2478)

at java.awt.Component.dispatchEvent(Component.java:4460)

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 persistencia.ServicoDao.atualizar(ServicoDao.java:159)

at windows.alterar.AlterarServico.salvarActionPerformed(AlterarServico.java:315)

at windows.alterar.AlterarServico.access$400(AlterarServico.java:35)

at windows.alterar.AlterarServico$3.actionPerformed(AlterarServico.java:217)

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:6263)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

at java.awt.Component.processEvent(Component.java:6028)

at java.awt.Container.processEvent(Container.java:2041)

at java.awt.Component.dispatchEventImpl(Component.java:4630)

at java.awt.Container.dispatchEventImpl(Container.java:2099)

at java.awt.Component.dispatchEvent(Component.java:4460)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)

at java.awt.Container.dispatchEventImpl(Container.java:2085)

at java.awt.Window.dispatchEventImpl(Window.java:2478)

at java.awt.Component.dispatchEvent(Component.java:4460)

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)

Alguém pode me ajudar?

O que devo fazer, será o mapeamento?

Desde já Valeu!

4 Respostas

romarcio

Posta ai o método que vc criou para o update.

A
public void atualizar(Tipo obj) throws Exception {

session = HibernateUtil.getSessionFactory();

try{

tx = session.beginTransaction();

session.update(obj);

tx.commit();

}

catch(Exception e){

if(tx != null)

tx.rollback();

e.printStackTrace();

throw e;

}

finally{

session.close();

}

Tipo::

package entidades;

import java.util.HashSet;
import java.util.Set;

public class Servico implements java.io.Serializable {

private int id;
 private Secao secao;
 private String descricao;
 private double valor;
 

public Servico() {
}


public Servico(int id, Secao secao, String descricao, double valor) {
    this.id = id;
    this.secao = secao;
    this.descricao = descricao;
    this.valor = valor;
}


public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}
public Secao getSecao() {
    return this.secao;
}

public void setSecao(Secao secao) {
    this.secao = secao;
}
public String getDescricao() {
    return this.descricao;
}

public void setDescricao(String descricao) {
    this.descricao = descricao;
}
public double getValor() {
    return this.valor;
}

public void setValor(double valor) {
    this.valor = valor;
}

}

}

:: Mapeamento .XML

<?xml version="1.0" encoding="UTF-8"?> servico_id_seq
<many-to-one class="entidades.Secao" column="secao" lazy="false" name="secao"/>
< query name="buscarPorNome">
  from Servico s where s.descricao like :nome
</query>
<query name="buscarPorNomeExato">
  from Servico s where s.descricao = :nome
</query>
<query name="listarTodos">
  from Servico as s order by s.descricao
</query>
<query name="listarTodosPorId">
  from Servico as s order by s.id
</query>
romarcio

Quando postar código, posta ele dentro das tags CODE, dai fica assim, mais fácil para vizualizar.

public void atualizar(Tipo obj) throws Exception {
session = HibernateUtil.getSessionFactory();
try{
tx = session.beginTransaction();
session.update(obj);
tx.commit();
}
catch(Exception e){
if(tx != null)
tx.rollback();
e.printStackTrace();
throw e;
}
finally{
session.close();
}

--------------------------------------------------------------------------------------------------------------------------------------------------

Tipo::

package entidades;



import java.util.HashSet;
import java.util.Set;


public class Servico implements java.io.Serializable {


private int id;
private Secao secao;
private String descricao;
private double valor;


public Servico() {
}


public Servico(int id, Secao secao, String descricao, double valor) {
this.id = id;
this.secao = secao;
this.descricao = descricao;
this.valor = valor;
}


public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}
public Secao getSecao() {
return this.secao;
}

public void setSecao(Secao secao) {
this.secao = secao;
}
public String getDescricao() {
return this.descricao;
}

public void setDescricao(String descricao) {
this.descricao = descricao;
}
public double getValor() {
return this.valor;
}

public void setValor(double valor) {
this.valor = valor;
}



}






}

-------------------------------------------------------------------------

:: Mapeamento .XML


&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
&lt;!-- Generated 22/07/2010 00:12:03 by Hibernate Tools 3.2.1.GA --&gt;
&lt;hibernate-mapping&gt;
&lt;class name="entidades.Servico" schema="gpro" table="servico"&gt;
&lt;id name="id" type="int"&gt;
&lt;column name="id"/&gt;
&lt;generator class="sequence"&gt;
&lt;param name="sequence"&gt;servico_id_seq&lt;/param&gt;
&lt;/generator&gt;
&lt;/id&gt;

&lt;many-to-one class="entidades.Secao" column="secao" lazy="false" name="secao"/&gt;

&lt;property name="descricao" type="string"&gt;
&lt;column length="80" name="descricao" not-null="true"/&gt;
&lt;/property&gt;

&lt;property name="valor" type="double"&gt;
&lt;column name="valor" not-null="true" precision="17" scale="17"/&gt;
&lt;/property&gt;

&lt; query name="buscarPorNome"&gt;
from Servico s where s.descricao like :nome
&lt;/query&gt;
&lt;query name="buscarPorNomeExato"&gt;
from Servico s where s.descricao = :nome
&lt;/query&gt;
&lt;query name="listarTodos"&gt;
from Servico as s order by s.descricao
&lt;/query&gt;
&lt;query name="listarTodosPorId"&gt;
from Servico as s order by s.id
&lt;/query&gt;


&lt;/class&gt;
&lt;/hibernate-mapping&gt;

Vc está usando SEQUENCE, acredito que o problema possa ser na sequencia. Quando vc utiliza sequence, não deve inserir ou alterar nada no banco direto SGDB, apenas pela aplicação. Por que senão, o hibernate perde a ordem sequencia .
Atualize a sequencia no banco de dados, ou deleta ela, e depois cria de novo.

A

Olá,
muito grato pela atenção, consegui solucionar, depois de muita luta, o tal problema. estava passando para as variáveis do Funcionario a ser atualizado, mas esquecendo do id, então ele estava retornando 0 ao buscar um funcionário com tais caracteristicas, já que sua chave primária é o id. Setei o parametro do id e funcionou,

At.te,

Criado 31 de julho de 2010
Ultima resposta 2 de ago. de 2010
Respostas 4
Participantes 2