Basic save question with dateCreated

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Basic save question with dateCreated

jasons
I have a controller accepting a POST with a JSON body, the JSON is used to create an object and the object is saved, then a response, also in JSON, is composed and returned.  Currently all the work is done in the controller (haven't moved it out to services yet).

It all works, the object is saved to the database fine, but when I compose the return JSON I get a null for the dateCreated property.  Even if I log it directly out it's null.  All the other properties of the object work fine.

Foo foo = new Foo()
foo.name = json.test
foo.validate()
if (foo.hasErrors()) { //compose a different kind of response and return}
foo.save()
log.info foo.dateCreated

That will show "null".  I can check the database after I do the request and it has a value there.

If I change the save to foo.save(flush: true) then dateCreated is populated.  Does dateCreated require a back and forth to the database in order to be populated?  That seems a little strange.

Basic question, but want to make sure I understand what's going on, I've usually not used flush: true unless I need something in the database right away (i.e. other requests need to know about it at the moment it happens).  

Thanks in advance.