org.hibernate.StaleObjectStateException:行已被另一个事务更新或删除(或未保存的值映射不正确)

时间:2015-11-25 11:55:05

标签: hibernate grails gorm

我知道这个错误在这里很常见。但我只是不明白为什么当我尝试删除一个实例时会发生这种情况。

<g:form url="[resource:scholarshipsForPeriodInstance, action:'delete']" method="DELETE">
            <fieldset class="buttons">
                <g:link class="edit" action="edit" resource="${scholarshipsForPeriodInstance}"><g:message code="default.button.edit.label" default="Edit" /></g:link>
                <g:actionSubmit class="delete" action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" />
            </fieldset>
        </g:form>
@Transactional
def delete(ScholarshipsForPeriod scholarshipsForPeriodInstance) {

    if (scholarshipsForPeriodInstance == null) {
        notFound()
        return
    }

    scholarshipsForPeriodInstance.delete flush: true
    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'ScholarshipsForPeriod.label', default: 'ScholarshipsForPeriod'), scholarshipsForPeriodInstance.id])
            redirect action: "index"
        }
        '*' { render status: NO_CONTENT }
    }
}

所以我打开logSql

Hibernate: delete from scholarships_for_period where id=? and version=?
Hibernate: update scholarships_for_period set version=?, application_name_id=?, apply_direct=?, donor_id=?, percentage_of_amount=?, period_id=?, restriction=?, total_amount=? where id=? and version=?

为什么在删除此实例后Hibernate会话尝试更新?我将不胜感激任何帮助。感谢。

==== 更新: 非常有趣的是,当我改变“重定向”时。来'渲染&#39;或者&#39;转发&#39;,然后没有错误,实例被删除。为什么???

@Transactional
def delete(ScholarshipsForPeriod scholarshipsForPeriodInstance) {
    if (scholarshipsForPeriodInstance == null) {
        notFound()
        return
    }

    scholarshipsForPeriodInstance.delete flush: true
    forward action: "index" //or render 'ok'
}

1 个答案:

答案 0 :(得分:0)

删除对象后,您正在使用资源scholarshipsForPeriodInstance.id - 一旦删除,您就不应再处理该对象了。