grails - Why don't unflushed domain objects revert to their "saved" state after discard? Can I get the "clean" version? -


confused here domain objects in intermediate states.

so here's class:

class foo {     double price     string name     date creationdate = new date()      static constraints = {         price min: 50d         creationdate nullable: false     } } 

and test case:

@testfor(foo) class foospec extends specification {    void "test creation , constraints"() {         when: "i create foo within constraints"             foo f= new foo(price: 60d, name: "foobar")         then:  "it validates , saves"             f.validate()             f.save()         when: "i make foo invalid"             f.price=49d         then: "it no longer validates"             !f.validate()         when: "i discard changes since last save"             f.discard()         then: "it validates again"             f.validate()                //test fails here     } } 

so test fails on last validate, because f.discard() doesn't seem revert f "saved" state. notes on grails docs appear indcate intended (alothough confusing me @ least) behavior. "discard" mark not persisted (which wouldn't anyway if fails constraints, in case this, guess nothing right? ) thought refresh might me saved state, or foo.get([id]) none of works. foo.get gets corrupted version , f.refresh throws nullpointer apparently because creationdate null , shouldn't be.

so confusing, , seems there's no way saved state of object if hasn't been flushed. true? sort of raises question "in respect object "saved?"

that seem mean want making sure i'm reading db not cache if want sure i'm getting valid persisted state of object, \not possibly corrupted intermediate state.

a related question -- not sure scope of local cache either -- restricted session? if start session , retrive id, i'll null because never saved, not corrupt version invalid price?

would appreciate explaining underlying design principal here.
thanks.

hibernate doesn't have process reverting state, although seems since keeps clean copy of data dirty checking. calling gorm discard() method calls few layer methods along way real work done in hibernate's sessionimpl.evict(object) method, , removes state session caches , disassociates instance session, nothing persistent properties in instance. if want undo changes should evict instance discard() , reload it, e.g. f = foo.get(f.id)


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -