今天在使用hibernate update的时候,抛了这样的一个异常。
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1 ...
对hibernate底层还不甚熟悉,不知道怎样解决,以及它发生的原因是什么?是否是hibernate真的当发现被update的po没有做任何修改时,就会抛出这个?
下面是具体的代码:
Product product = (Product)service().getById(Product.class, Long.valueOf(id));
copy(product,form); //将form中数据拷贝到product
product.setId(Long.valueOf(id));
service().update(product);
期待各位的回答!
google里面的说明是,
You tried to delete an object that was allready deleted from the database.
会出现这个。
当我这里已经前后的逻辑都没有删除的操作。
[b]问题补充:[/b]
你不能这么修改主键吧
这个id的值是一样的。也会有影响么?会影响hibernate对象的状态?
可能会导致hibernate以为你要更新一个非持久化对象,
所以,你最好不要product.setId(Long.valueOf(id)); 这行
你不能这么修改主键吧
Product product = (Product)service().getById(Product.class, Long.valueOf(id));
上面既然查了一遍了,上面已经是你查好的对象了,下面为什么又注入一遍id呢?
还有问题我觉得主要出在了Long.valueOf(id)看看你的hbm.xml中的主键是配置的什么类型的吧
product.setId(Long.valueOf(id));