tommycat
tommycat

Reputation: 29

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

I know this error is very common here. But I just didn't understand why it happens when I try to delete a instance.

<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 }
    }
}

so I turn on the 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=?

why Hibernate session try to update after I delete this instance? I would be grateful for any help. Thanks.

==== UPDATE: very funny is that when I change 'redirect' to 'render' or 'forward', then there is no error coming and the instance is deleted. Why???

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

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

Upvotes: 2

Views: 1556

Answers (1)

Raz Abramov
Raz Abramov

Reputation: 191

You are using the resource scholarshipsForPeriodInstance.id after deleting the object - once deleted, you should not address the object anymore.

Upvotes: 0

Related Questions