希望这将是一个简单的一个回答。我创建Grails中称为播放器的一类,其具有这样的信息:

class Player {
 String steamId
 String name
 String portrait
 static hasMany = {playerStatistics:PlayerStatistics}
 static hasOne = {playerForumProfile:PlayerForumProfile}
}

有关澄清,玩家对象可以具有一个PlayerForumProfile对象,但玩家对象总是创建的 BEFORE 的PlayerForumProfile对象。我的问题是访问与PlayerForumProfile类的控制器内的“hasOne”属性相关联的playerForumProfile对象。我本以为,这样做:

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

    playerForumProfileInstance = playerInstance.playerForumProfile

将导致牵引与playerInstance物体插入playerForumProfileInstance变量相关联的对象PlayerForumProfile然而当我尝试此,Grails的引发错误告诉我不存在这样的属性如playerForumProfile。是否有可能访问hasOne属性对象以这样的方式或做我需要做点别的?

编辑:我还试图修改播放器类,所以它包括称为可变的 playerForumProfile 并编辑PlayerForumProfile所以它有一个的属于关联声明,但这种保持产生了空运行我的应用程序时指针异常。

编辑:阿多一点信息,我创建了一个从无到有的新的Grails应用程序,并创建关系似乎Grails文档的方式并没有问题跑了,所以我想它可能是更容易只是开始一个新的应用程序和文件复制过来。

有帮助吗?

解决方案

这答案不再是Grails的2.X和更大的正确的话,2009年是真正的原回答的时候。

有未处于GORM一个 “hasOne” 属性,它要么属于关联:

static belongsTo = [playerForumProfile: PlayerForumProfile]

或只是属性名称的规则类型化定义,如果没有由属于关联暗示级联关系:

PlayerForumProfile playerForumProfile

请参阅一个-to-one的细节GORM文档

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top