Reputation: 2975
What I'd like to do is to set:
setReadOnly(true)
to every criteria query by default.
Is it possible to define default settings that would be applied to every single criteria query that is executed in the application?
P.S. I would possibly want to add the following as criteria defaults also but am unsure, whether they would have any additional effect to setReadOnly:
setCacheMode(CacheMode.IGNORE)
setFlushMode(FlushMode.MANUAL)
Upvotes: 1
Views: 1760
Reputation: 8587
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
Upvotes: 2