I am looking for a "isDeleted()" test for Grails (GORM) instances:
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.
You need to get to the Hibernate session and persistence context:
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, e.g.
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:
if (Project.exists(p.id)) {
// do something....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With