Why do non-unlocked domain objects return to their "saved" state after a reset? Can I get a "clean" version?

This is where domain objects are tangled in intermediate states.

So here's the class:

class Foo {
    Double price
    String name
    Date creationDate = new Date()

    static constraints = {
        price min: 50D
        creationDate nullable: false
    }
}

      

And this test case:

@TestFor(Foo)
class FooSpec extends Specification {
   void "test creation and constraints"() {
        when: "I create a Foo within constraints"
            Foo f= new Foo(price: 60D, name: "FooBar")
        then:  "It validates and saves"
            f.validate()
            f.save()
        when: "I make that foo invalid"
            f.price=49D
        then: "It no longer validates"
            !f.validate()
        when: "I discard the changes since the last save"
            f.discard()
        then: "it validates again"
            f.validate()                //TEST FAILS HERE
    }
}

      

So this test fails on the last confirmation, as f.discard () doesn't seem to return f to its "saved" state. The grails docs notes seem to indicate that this is intended (albeit very confusing to me at least) behavior. What all "throws away" does indicates that it shouldn't be saved (what would it be in no way if it didn't work with constraints, so in that case, I guess it's not doing anything right?) I thought the update might get me the saved state or even Foo.get ([id]), but none of this works. Foo.get gets a corrupted version and f.refresh throws a nullPointer, presumably because creationDate is null and it shouldn't.

So, this is all confusing and it looks like there is no way to revert to the saved state of the object unless it has been cleaned up. Is it really so? Which species raises the question "In what respect is the object preserved?"

This would seemingly mean that I would like to make sure that I am definitely reading from the db not the cache if I want to be sure that I am getting a valid persistent state of the object, \ not some possible corrupted intermediate state.

Related question - Not sure if the local cache scope is a session limit? So if I start another session and return this ID, I get zero because it was never saved, not a corrupt version with an invalid price?

Would thank someone explaining the underlying design principle here. Thank.

+3


source to share


1 answer


Hibernate does not have a state return process, although it seems to be possible, since it keeps a clean copy of the data for dirty checking. The GORM method call discard()

invokes multiple layer methods along the way, but the real work is done in the Hibernate method SessionImpl.evict(Object)

and that just clears the state from the session caches and disconnects the session instance, but does nothing for the persistent properties on the instance. If you want to undo the changes, you have to dump the instance with discard()

and reload it eg.f = Foo.get(f.id)



+2


source







All Articles