这是 Grails 瞬态的有效使用吗?
我有一个域对象,我想在其中存储一些仅在运行时存在的东西。我查看了 文档 并找到了 transients
关键字,从表面上看,这正是我正在寻找的。这是我的域对象的样子...
class Contact {
def Seeker
def beforeInsert()
{
initiatedDate = new Date()
}
Date initiatedDate
Date acceptedDate
static transients = ['pal']
Seeker pal
}
其中 Seeker
是一个 groovy 类,它不是域对象,而是某些属性的占位符。
到目前为止,一切都很好,我的联系人表没有预期的 pal 字段。在我的 ContactController
中,我查询一堆联系人 c
,然后查找他们的 Seeker
好友(如何保留的详细信息)并设置 < code>pal 字段到一个新对象。
c.pal = new Seeker();
c.pal.name = otherObject.name
render c as JSON
除了返回的 JSON 中缺少 pal 对象之外,这一切似乎都工作正常。
这是瞬态的有效使用吗?文档提到它们对于基于函数的 getter 和 setter 来说很方便,但就我而言,我想要一个实际的对象。我应该在我的对象上编写 getPal() 和 setPal() 方法吗?
谢谢
I have a domain object on which I want to store a few things which only exist at runtime. I looked at the documentation and found the transients
keyword, which, on the face of it was what I was looking for. Here is what my domain object looks like...
class Contact {
def Seeker
def beforeInsert()
{
initiatedDate = new Date()
}
Date initiatedDate
Date acceptedDate
static transients = ['pal']
Seeker pal
}
where Seeker
is a groovy class which is not a domain object, but a placeholder for some properties.
So far all is fine and my Contact table does not have a pal
field as expected. In my ContactController
I query for a bunch of contacts, c
, then look up their Seeker
pals (details of how withheld) and set the pal
field to a new object.
c.pal = new Seeker();
c.pal.name = otherObject.name
render c as JSON
This all seems to work fine except that the pal object is missing from the JSON returned.
Is this a valid use of transients? The docs mention that they are handy for function-based getters and setters, but in my case I want an actual object. Should I be writing a getPal() and setPal() method on my object?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
瞬态确实用于阻止域对象中的字段被持久化。 (如果您想在
pal
字段上执行一些初始化,而不必将其放入控制器中,您可以使用onLoad()
事件或编写getPal( )
方法,它将覆盖默认属性 getter)。您还注意到默认的 JSON 编组器仅呈现持久字段。当渲染我的域对象时,我发现创建 JSON 对象编组器很有用,这样就不会渲染不需要的属性,但它也可以解决您的瞬态问题。您可以使用
JSON.registerObjectMarshaller
方法来执行此操作:如果您在
BootStrap.groovy
中添加编组器,它们将在您的控制器中可用。HTH
(还发现了这个:http://old. nabble.com/Taggable-plugin-and-JSON-converter-ts24830987.html#a24832970)
Transients are indeed used to stop fields in domain objects from being persisted. (If you want to perform some init on the
pal
field without having to put it in your controller you could use theonLoad()
event or write agetPal()
method which would override the default property getter). You are also right to note that the default JSON marshaller only renders persisted fields.When rendering my domain objects I've found it useful to create JSON object marshallers so that unwanted properties are not rendered, but it would also solve your transient issue too. You can do this using the
JSON.registerObjectMarshaller
method:If you add the marshallers in your
BootStrap.groovy
they will be available in your controllers.HTH
(Also found this: http://old.nabble.com/Taggable-plugin-and-JSON-converter-ts24830987.html#a24832970)