简体   繁体   中英

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)?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM