How does Grails GORM MongoDB decide when to save my objects?
With Grails 2.2.4 + mongodb:1.3.0, I'm observing some strange behavior
with respect to how my domain objects are persisted. Some of this may be
technically correct, but some of it I find really perplexing.
I'd love some insight into why each of the following three things is
happening.
1. Stateful domain objects save themselves automatically (?)
If I use a stateful (default) domain object:
class Item {
ObjectId id
}
When I create a new domain object in my controller, then even if I never
call save or insert...
i = new Item()
i.someSchemalessProperty = "a schemaless value!"
Then the new item lands in my database, with an _id and
someSchemalessProperty both set. Is this expected behavior?
Now if I use a stateless domain object...
class Item {
ObjectId id
static mapping = {
stateless true
}
}
2. Stateless domain objects auto-save if manipulated after insert (?)
i = new Item()
i.insert(flush:true)
i.someSchemalessProperty = "a schemaless value!"
The result is an item in my database with _id and someSchemalessProperty
both set. (I find this a bit strange.)
3. Stateless domain objects... act up... if you don't call insert ASAP (?)
i = new Item()
i.someSchemalessProperty = "a schemaless value!"
i.insert(flush:true)
The result is an item in my database with _id, but no
someSchemalessProperty set. (I find this a bit strange.)
That's utterly perplexing.
No comments:
Post a Comment