[英]How can I tell if a Grails/GORM domain instance has been deleted in the current tx (Hibernate)?
I am looking for a "isDeleted()" test for Grails (GORM) instances: 我正在寻找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 ...
In my app the logic that may delete p is elsewhere and passing a flag back or something would be a pain. 在我的应用程序中,可能删除p的逻辑在其他地方并且传回一个标志或某些东西会很痛苦。
You need to get to the Hibernate session and persistence context: 您需要进入Hibernate会话和持久化上下文:
import org.hibernate.engine.Status
boolean deleted = Project.withSession { session ->
session.persistenceContext.getEntry(p).status == Status.DELETED
}
You could use GORM events to automatically set a property within the object once it has been deleted, eg 您可以使用GORM事件在删除对象后自动设置属性,例如
class Project{
String name
Boolean isDeleted = false
static transients = [ "isDeleted" ]
def afterDelete() {
isDeleted = true
}
}
If for some reason you don't want to modify the domain classes, you could just use the exists
method: 如果由于某种原因您不想修改域类,则可以使用
exists
方法:
if (Project.exists(p.id)) {
// do something....
}
my thoughts: 我的想法:
Project p = ...
def id = p.id
p.delete(flush:true)
...
if (p.read(id)) //... do some more stuff ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.