如何判断当前tx(Hibernate)中是否删除了Grails/GORM域实例?

Dav*_*ker 5 java grails hibernate grails-orm

我正在寻找Grails(GORM)实例的"isDeleted()"测试:

Project p = ... get persistent entity from somewhere ...
p.delete() // done in some nested logic
... sometime later in the code prior to commit of the tx ...
if (!p.isDeleted()) ... do some more stuff ...
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,可能删除p的逻辑在其他地方并且传回一个标志或某些东西会很痛苦.

Bur*_*ith 6

您需要进入Hibernate会话和持久化上下文:

import org.hibernate.engine.Status

boolean deleted = Project.withSession { session ->
   session.persistenceContext.getEntry(p).status == Status.DELETED
}
Run Code Online (Sandbox Code Playgroud)