我如何在Grails中使用嵌入的GORM类? [英] How can I use embedded GORM classes in Grails?

查看:115
本文介绍了我如何在Grails中使用嵌入的GORM类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循 GORM文档,我尝试使用以下内容使用Grails 2.2.1的域类:

Following the GORM docs I tried to use the following domain class with Grails 2.2.1:

package grailscompositiontest

class ScafPerson {
    String name
    ScafAddress homeAddress
    ScafAddress workAddress

    static constraints = {
        name(nullable: false, blank: false)
    }

    static embedded = ['homeAddress', 'workAddress']
}

class ScafAddress {
    String number
    String code
}

控制器只使用脚手架:

The controller just uses scaffolding:

package grailscompositiontest

class ScafPersonController {
    static scaffold = true
}

不幸的是,这不起作用,只要我浏览到创建视图,就会触发服务器错误:

Unfortunately this does not work, it triggers a server error as soon as I browse to the "create" view:

URI:     /GrailsCompositionTest/scafPerson/create
Class:   java.lang.NullPointerException
Message: Cannot get property 'id' on null object

任何想法我做错了什么?

Any idea what I am doing wrong?

推荐答案

我发现grails文档说将嵌入类定义与域类放在同一个源文件中,或者将其放入src / groovy中。我把Address.groovy放在src / groovy中,并为我的Family类生成了UI,它有一个嵌入对象。生成的创建页面现在正确显示,地址字段包含在族页面中。但是,我仍然需要修改生成的_form.gsp中每个表单域的名称和值域。

I found that the grails documentation says to put embedded class definitions either the same source file as the domain class or put it into src/groovy. I put Address.groovy in src/groovy and generated the UI for my Family class, which has an embedded object. The generated create page now displays correctly with the address fields are included in the Family page. However, I still had to modify the name and the value fields of each form field in the generated _form.gsp.

例如,以下生成的代码...

For example, the following generated code...

<g:textField name="line1" required="" value="${addressInstance?.line1}"/>

已更改为...

<g:textField name="address.line1" required="" value="${familyRegistrationInstance.address?.line1}"/>

其中主要域对象是FamilyRegistration域类(在生成的_form.jsp中称为familyRegistrationInstance ),它有一个嵌入的地址对象。我正在使用grails 2.3.11。

Where the primary domain object is a FamilyRegistration domain class (referred to in the generated _form.jsp as familyRegistrationInstance), which has an embedded Address object. I am using grails 2.3.11.

保存操作现在可以正确填充嵌入对象的字段。在我的情况下,这意味着FamilyRegistration.address正确填充。

The save operation now works with the fields of the embedded object populated correctly. In my case, this means that FamilyRegistration.address is populated correctly.

以下是来自Grails参考文档的相关信息...

Here's the related info from the Grails Reference document...

嵌入式组件类通常在与拥有的类相同的源文件中声明,或者在src / groovy下的自己的文件中声明。您可以将组件类放在grails-app / domain下,但是如果所以Grails会自动为它创建一个专用的表格,将这个类放在src / groovy下通常是最好的选择,因为你可以在多个域类中共享这个组件。

"The embedded component class is typically declared in the same source file as the owning class or in its own file under src/groovy. You can put the component class under grails-app/domain, but if you do so Grails will automatically create a dedicated table for it. Putting the class under src/groovy is usually the best option because you can then share the component across multiple domain classes."

http://grails.org/doc/2.2.4/ ref / Domain%20Classes / embedded.html

这篇关于我如何在Grails中使用嵌入的GORM类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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