圣杯 "Cannot set readonly property"

Grails "Cannot set readonly property"

我有 2 个多对多关系的域,中间 table 这个关系

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
    }
}

middle table with Song and SongList id 用于存储关系

class HasSongListSong {

    static belongsTo = [SongList, Song]

    SongList songList
    Song song

    static constraints = {
    }
}

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

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

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

你没有设置 id,你设置了对象:

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

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