Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: what does .save(flush:flush, insert:true) do differently from .save(flush:true)

In the spring security generated class UserRole or SecUserSecRole (you could call it whatever you choose) there is a command to make a new UserRole() and save it with .save(flush:flush, insert:true)

What does that mean? What is it doing differently from .save(flush:true)?

like image 748
Mikey Avatar asked Feb 15 '12 21:02

Mikey


2 Answers

From the Grails docs:

insert (optional) - When set to true will force Hibernate to do a SQL INSERT, this is useful in certain situations when legacy databases (such as AS/400) are involved and Hibernate cannot detect whether to do an INSERT or an UPDATE

like image 90
doelleri Avatar answered Nov 01 '22 15:11

doelleri


People usually use flush() because they are uncomfortable with the way Hibernate works.

If you need to know more about this, check this out http://blog.springsource.com/2010/06/23/gorm-gotchas-part-1/

There are times when you need to however, specifically when you are working in the same thread as the save() call and you need to be sure that the Domain object is persisted into the DB. Also, some constraints in your domain use the database to check if the data is valid or not. It is sometimes necessary to make sure validation works. There is an example of that here :

http://johnrellis.blogspot.com/2009/09/grails-constraints-across-relationships.html

So basically, you should only use flush if you really need something in the DB RIGHT NOW! This is less times than you would think. Hope this help, and not a single bit of toilet humour in a conversation about flushing... so proud:)

URL: http://grails.1312388.n4.nabble.com/When-to-use-domain-save-flush-true-or-domain-save-td2289869.html

like image 4
Eduardo Cuomo Avatar answered Nov 01 '22 17:11

Eduardo Cuomo