Grails - BootStrap.groovy - 空指针问题

时间:2012-10-24 06:25:33

标签: grails groovy bootstrapping grails-2.1

加载开发数据的BootStrap.groovy存在问题。之前它总是加载数据,但现在已经停止在运行grails run-app时抛出以下错误。

Message: Validation Error(s) occurred during save():
- Field error in object 'spotlight.content.Profile' on field 'portfolio': rejected value  [null];

我的2个模型如下;

class Portfolio {
   Profile profile

String portfolioName
String portdescrip
Integer portpublished
Date dateCreated
Date lastUpdated

class Profile {
   static belongsTo = [portfolio: Portfolio]

String portfoliohtml
String portfolioEmail
String portfoliocc
String portfolioAdmin
String portfolioFilestore
String portfolioColor
String bugzillaproduct
String bugzillacomponent
String rtqueue
String teamqueueemail
String etherpadurl
Integer siteupload
Date dateCreated
Date lastUpdated

在BootStrap.groovy文件中,我有以下内容;

import java.util.Date;
import spotlight.content.Profile
import spotlight.content.Portfolio

class BootStrap { 

def init = { servletContext ->

    def profile = new Profile(portfoliohtml:"No",
            portfolioEmail: "ian@ian.com",
            portfolioAdmin:"Ian Neilsen",
            bugzillaproduct:"bz prod name",
            bugzillacomponent:"comp name",
            siteupload:1,
            portfoliocc: "ian@ian.com",
            portfolioColor:"red",
            portfolioFilestore:"blah",
            rtqueue:"queue name",
            teamqueueemail:"ian@ian.com",
            etherpadurl:"http://url.com",
            ).save(failOnError: true)

    def portfolio = new Portfolio(portfolioName:"Portfolio 1",
                            portdescrip:"portfolio descrition field",
                            portpublished:1,
                            portfolio:profile).save(failOnError: true)

}

我已经尝试过将我的个人资料对象添加到投资组合对象的每一个化身都没有任何运气。正如我之前所说,这有效,现在已经停止抛出零错误。

让我难过任何想法?

欢呼声

1 个答案:

答案 0 :(得分:2)

看起来你有几个错误。 一个(但不是导致错误消息)是,您尝试将profile实例添加到portfolio实例的portfolia属性。 Portfolio没有属性portfolio

关于您的错误消息,请尝试以下操作:

def portfolio = new Portfolio(portfolioName:"Portfolio 1", ...)
portfolio.profile = new Profile(...)
portfolio.save(failOnError: true)

如需进一步阅读,请查看grails docs的Many-to-one and one-to-one (GORM)部分。