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

怎甘沉沦 提交于 2019-12-10 17:08:15

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/9301395/grails-what-does-saveflushflush-inserttrue-do-differently-from-saveflus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!