Grails形成了多对一belongsTo的创建

时间:2013-11-24 06:46:59

标签: grails grails-2.0 gsp

我是grails的初学者,我有一个基本问题。我想在子类表单创建中显示父类的实例列表。

My Domain类如下。 Parent类是公司。

class Company {
    String name
    static constraints = {
        name(blank:false)
    }
    String toString(){name}
}

我的孩子班是该公司的所在地。

class Location {
    String name
    String address
    static belongsTo= {companyLocation:Company}

    static constraints = {
    name(blank: false)
    address blank:false 
}

    String toString(){"company:"+companyLocation+"Location:"+name}
}

现在在_form template' of location view I have the code for the companyLocation下拉列表中

<div class="fieldcontain ${hasErrors(bean: locationInstance, field: 'companyLocation', 'error')} required">
<label for="companyLocation">
<g:message code="location.companyLocation.label" default="companyLocation" />
    <span class="required-indicator">*</span>
    <g:select id="companyLocation" name="companyLocation.id" from="${first_project.Company.list()}" optionKey="id" required="" value="${locationInstance?.companyLocation?.id}" class="many-to-one"/>
</label>
</div>

当我进入创建页面时,我收到错误:

Error 500: Internal Server Error
URI /first_project/location/create
Class  groovy.lang.MissingPropertyException
Message No such property: companyLocation for class: first_project.Location

为什么在companyLocation类中定义静态变量Location Domain时会出现此错误?有人可以告诉我哪里出错了吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

这看起来像语法问题,

static belongsTo= {companyLocation:Company}

应该是

static belongsTo= [companyLocation:Company]

答案 1 :(得分:0)

还有一种OO方法可以做到这一点,而不是使用多种方法而属于......

创建另一个CompanyLocation Domain类。

Class CompanyLocation {
   Company company
   Location location

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

public String toString() {
    return "${company} ${location}"
}

}