错误内容如下:
Hibernate: update test.dbo.userinfo set username=?, password=? where id=?
Exception in thread "main" org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
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:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at gwm.com.Useroperation.update(Useroperation.java:26)
at gwm.com.Usermain.main(Usermain.java:19)
数据库是SQl2000表中有三个字段id(主键,自动增长),username,password.插入,删除,查询,都没有问题,唯到更新这里就报错。这是对应表的设置:
<hibernate-mapping>
<class name="gwm.com.Userinfo" table="userinfo" schema="dbo" catalog="test">
<id name="id" type="integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="username" type="string">
<column name="username" length="20" />
</property>
<property name="password" type="string">
<column name="password" length="20" />
</property>
</class>
</hibernate-mapping>
Hibernate: update test.dbo.userinfo set username=?, password=? where id=?
Exception in thread "main" org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
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:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at gwm.com.Useroperation.update(Useroperation.java:26)
at gwm.com.Usermain.main(Usermain.java:19)
数据库是SQl2000表中有三个字段id(主键,自动增长),username,password.插入,删除,查询,都没有问题,唯到更新这里就报错。这是对应表的设置:
<hibernate-mapping>
<class name="gwm.com.Userinfo" table="userinfo" schema="dbo" catalog="test">
<id name="id" type="integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="username" type="string">
<column name="username" length="20" />
</property>
<property name="password" type="string">
<column name="password" length="20" />
</property>
</class>
</hibernate-mapping>
public void update(Userinfo ui)
{
Transaction tran = session.beginTransaction();
this.session.update(ui);
tran.commit();
this.session.close();
}
NonBatchingBatcher类里有毛病 修改源码或重新倒入hibernate的包替换NonBatchingBatcher.java如下:
//$Id: NonBatchingBatcher.java,v 1.7 2005/12/06 22:27:12 oneovthafew Exp $
package org.hibernate.jdbc;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.StaleStateException;
/**
* An implementation of the <tt>Batcher</tt> interface that does no batching
*
* @author Gavin King
*/
public class NonBatchingBatcher extends AbstractBatcher {
public NonBatchingBatcher(ConnectionManager connectionManager, Interceptor interceptor) {
super( connectionManager, interceptor );
}
public void addToBatch(int expectedRowCount) throws SQLException, HibernateException {
final int rowCount = getStatement().executeUpdate();
//negative expected row count means we don't know how many rows to expect
if ( expectedRowCount>0 ) {
if ( expectedRowCount>rowCount ) {
// throw new StaleStateException(
// "Unexpected row count: " + rowCount +
// " expected: " + expectedRowCount
// );
log.info("Unexpected row count: " + rowCount +
" expected: " + expectedRowCount);
}
if ( expectedRowCount<rowCount ) {
// throw new HibernateException(
// "Unexpected row count: " + rowCount +
// " expected: " + expectedRowCount
// );
log.info("Unexpected row count: " + rowCount +
" expected: " + expectedRowCount);
}
}
}
protected void doExecuteBatch(PreparedStatement ps) throws SQLException, HibernateException {
}
}
根据这一句好象你更新的对象在数据库中根本就没有对应的记录,检查一下对象的id是否正确
以上纯属个人见解,哪里不对大家别骂