使用静态;“哈松”;Grails控制器类中的属性

使用静态;“哈松”;Grails控制器类中的属性,grails,domaincontroller,Grails,Domaincontroller,希望这将是一个容易回答的问题。我在Grails中创建了一个名为player的类,该类包含以下信息: class Player { String steamId String name String portrait static hasMany = {playerStatistics:PlayerStatistics} static hasOne = {playerForumProfile:PlayerForumProfile} } 为了澄清,一个Player对象可以有一个Playe

希望这将是一个容易回答的问题。我在Grails中创建了一个名为player的类,该类包含以下信息:

class Player {
 String steamId
 String name
 String portrait
 static hasMany = {playerStatistics:PlayerStatistics}
 static hasOne = {playerForumProfile:PlayerForumProfile}
}
为了澄清,一个Player对象可以有一个PlayerForumProfile对象,但是Player对象总是在PlayerForumProfile对象之前创建的。我的问题是访问与playerForumProfile类控制器中的“hasOne”属性关联的playerForumProfile对象。我假设这样做:

    def playerForumProfileInstance = new PlayerForumProfile()
    def playerInstance = Player.get(params.id)

    playerForumProfileInstance = playerInstance.playerForumProfile
将导致将与playerInstance对象关联的PlayerForumProfile对象拉入PlayerForumProfile实例变量,但是当我尝试此操作时,Grails抛出一个错误,告诉我不存在PlayerForumProfile这样的属性。是否有可能以这种方式访问hasOne properties的对象,或者我需要执行其他操作

编辑:我还尝试修改Player类,使其包含一个名为playerForumProfile的变量,并编辑playerForumProfile,使其具有belongsTo声明,但在运行我的应用程序时,这不断导致空指针异常


编辑:还有一点信息,我从头开始创建了一个新的grails应用程序,并按照grails文档中显示的方式创建了关系,运行起来没有问题,因此我认为启动一个新应用程序并复制文件可能会更容易。

对于grails 2.X及更高版本,这个答案不再正确,2009年,最初的答案是正确的

GORM中没有“hasOne”属性,它要么属于:

static belongsTo = [playerForumProfile: PlayerForumProfile]
PlayerForumProfile playerForumProfile
或者,如果belongsTo没有隐含的级联关系,则仅为属性名的常规类型定义:

static belongsTo = [playerForumProfile: PlayerForumProfile]
PlayerForumProfile playerForumProfile

有关详细信息,请参见。

对于grails 2.X及更高版本,这个答案不再正确,最初的答案在2009年是正确的

GORM中没有“hasOne”属性,它要么属于:

static belongsTo = [playerForumProfile: PlayerForumProfile]
PlayerForumProfile playerForumProfile
或者,如果belongsTo没有隐含的级联关系,则仅为属性名的常规类型定义:

static belongsTo = [playerForumProfile: PlayerForumProfile]
PlayerForumProfile playerForumProfile

有关详细信息,请参见。

GORM中有一个hasOne功能:

GORM中有一个hasOne功能:

我检查了文档,我想说的是,当我尝试他们的示例并运行我的应用程序时,在放置PlayerForumProfile对象(作为属性)时,我会得到一个空指针在Player类中。我检查了文档,我想说的是,当我尝试他们的示例并运行我的应用程序时,在Player类中放置PlayerForumProfile对象(作为属性)时,我会得到一个空指针。鉴于我的项目进展不太快,我将重建它。希望这将解决错误。事实上,从头开始重建程序解决了问题。鉴于我的项目进展不太快,我将重建它。希望这将解决错误。事实上,从头开始重建程序解决了问题。