How to set default behaviour to Grails' Criteria?

假如想象 提交于 2019-12-08 18:52:36

Done some digging and found this for you:

http://www.javacodegeeks.com/2012/10/stuff-i-learned-from-grails-consulting.html

One limitation of the read method is that it only works for instances loaded individually by id. But there are other approaches that affect multiple instances. One is to make the entire session read-only:

1   session.defaultReadOnly = true

Now all loaded instances will default to read-only, for example instances from criteria queries and finders.

A convenient way to access the session is the withSession method on an arbitrary domain class:

1   SomeDomainClass.withSession { session ->
2      session.defaultReadOnly = true
3   }

It’s rare that an entire session will be read-only though. You can set the results of individual criteria query to be read-only with the setReadOnly method:

1   def c = Account.createCriteria()
2   def results = c {
3      between('balance', 500, 1000)
4      eq('branch', 'London')
5      maxResults(10)
6      setReadOnly true
7   }

One significant limitation of this technique is that attached collections are not affected by the read-only status of the owning instance (and there doesn’t seem to be a way to configure collection to ignore changes on a per-instance basis).

Read more about this in the Hibernate documentation

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