Grails“无法设置只读属性”

时间:2015-04-13 19:23:01

标签: grails many-to-many

我有两个关系多对多的域名,以及这种关系的中间表

class Song implements Taggable {

    static belongsTo = [User]

    static hasMany = [audios: Audio, couplets: Couplet, hasSongListSong: HasSongListSong, hasSublistSong: HasSublistSong]

    String title
    Boolean isChorusRepeat = false
    Boolean deleted = false
    Date dateCreated, lastUpdated

    static constraints = {
        title blank: false, size:1..129
    }
}

class SongList {

    static belongsTo = [User]
    static hasMany = [hasAccess: HasAccess, songListHasSong: HasSongListSong, songSubList: SongSubList]

    String title
    User user
    Boolean deleted = false
    Date dateCreated, lastUpdated

    static constraints = {
        title size: 3..128, blank: false, unique: false
    }
}

用于存储关系的Song和SongList id的中间表

class HasSongListSong {

    static belongsTo = [SongList, Song]

    SongList songList
    Song song

    static constraints = {
    }
}

当我尝试在此表中设置数据时,我得到了这个例外

def addSongs() {
    def hasSong = new HasSongListSong();
    hasSong.songId = Song.load(1)
    hasSong.songListId = SongList.load(1)
}

和第二个问题如果我从id的前端范围收到,如何更好地保存这种情况,我的意思是控制器来请求与songId 1,2的集合,.. n我应该使用一些迭代器或者有一些更好的方式?

1 个答案:

答案 0 :(得分:2)

您没有设置ID,您可以设置对象:

def addSongs() {
    def hasSong = new HasSongListSong();
    hasSong.song = Song.load(1)
    hasSong.songList = SongList.load(1)
}

作为一个新问题问你第二个问题,我可以尝试回答这个问题。

相关问题