如何将默认行为设置为Grails的标准? [英] How to set default behaviour to Grails' Criteria?

查看:95
本文介绍了如何将默认行为设置为Grails的标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是设置:

  setReadOnly(true)

$ b $ p
$ b

默认为每个标准查询。



是否可以定义将应用于在应用程序中执行的每个标准查询的默认设置?



PS
我可能想添加以下作为默认标准,但是我不确定,它们是否会对setReadOnly产生任何额外的影响:

  setCacheMode(CacheMode.IGNORE)
setFlushMode(FlushMode.MANUAL)


解决方案

完成了一些挖掘工作,并为您找到了它:

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

读取方法的一个局限性是,它只适用于由id单独加载的实例。但还有其他方法会影响多个实例。一个是让整个会话只读:

  1 session.defaultReadOnly = true 

$ b
$ b

访问会话的一种便捷方式是任意域类上的withSession方法:

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

会话将是只读的。您可以使用setReadOnly方法将个别条件查询的结果设置为只读:

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

这项技术的一个重要限制是附加的集合不受所拥有实例的只读状态的影响(并且似乎没有配置集合以忽略每个实例的更改的方式)。

请阅读 Hibernate文档


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)

解决方案

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

这篇关于如何将默认行为设置为Grails的标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆