这是 Grails 瞬态的有效使用吗?

发布于 2024-08-19 12:37:35 字数 931 浏览 5 评论 0原文

我有一个域对象,我想在其中存储一些仅在运行时存在的东西。我查看了 文档 并找到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2024-08-26 12:37:35

瞬态确实用于阻止域对象中的字段被持久化。 (如果您想在 pal 字段上执行一些初始化,而不必将其放入控制器中,您可以使用 onLoad() 事件或编写 getPal( ) 方法,它将覆盖默认属性 getter)。您还注意到默认的 JSON 编组器仅呈现持久字段。

当渲染我的域对象时,我发现创建 JSON 对象编组器很有用,这样就不会渲染不需要的属性,但它也可以解决您的瞬态问题。您可以使用 JSON.registerObjectMarshaller 方法来执行此操作:

import grails.converters.JSON
...
class BootStrap {

    def init = {servletContext ->
        JSON.registerObjectMarshaller(Contact ) {
            def returnArray = [:]
            returnArray['id'] = it.id
            returnArray['initiatedDate'] = it.initiatedDate
            returnArray['acceptedDate'] = it.acceptedDate
            returnArray['pal'] = it.pal
            return returnArray
        }

        JSON.registerObjectMarshaller(Seeker) {
            ...
        }

如果您在 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 the onLoad() event or write a getPal() 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:

import grails.converters.JSON
...
class BootStrap {

    def init = {servletContext ->
        JSON.registerObjectMarshaller(Contact ) {
            def returnArray = [:]
            returnArray['id'] = it.id
            returnArray['initiatedDate'] = it.initiatedDate
            returnArray['acceptedDate'] = it.acceptedDate
            returnArray['pal'] = it.pal
            return returnArray
        }

        JSON.registerObjectMarshaller(Seeker) {
            ...
        }

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文