dalew75
dalew75

Reputation: 99

What's the best way to ensure an instance of a domain class is not changed or modified

When I retrieve a domain instance via GORM, there is the possible danger of code modifying that instance/row. Especially because Grails automatically saves it even without calling .save() on the instance.

The only way I've ensured this before is making sure I call discard() on the instance in the past. Is there a better way?

This is for Grails 2.2.5

Upvotes: 0

Views: 57

Answers (2)

erichelgeson
erichelgeson

Reputation: 2340

What version of Grails/GORM?

An option would be do this work in a @ReadOnly (or @Transactional(readOnly = true)) Transaction - an exception will be thrown if an write operation is attempted.

Upvotes: 2

Daniel
Daniel

Reputation: 3370

If you want to always require manual .save() calls, that's possible by changing the hibernate.flush.mode though even that has its risks. Using .discard() is your best bet in most cases. As Anton points out, you can use .isDirty() to find out if anything has changed, but that will not prevent the save from occurring.

Upvotes: 0

Related Questions