我更新以下问题
我正在尝试在Grails中保存一系列嵌套的域对象,并且我遇到了与此语法相关的问题。
举个例子,假设我有以下嵌套的一对多关系:
Artist->相册 - >宋
我首先创建一个艺术家
Artist artist = new Artist(parameters)
artist.save()
使用新保存的艺术家,我希望保存一系列专辑及其相关的歌曲。
我认为这样的事情会奏效,但结果好坏参与。
Album album = new Album()
artist.addToAlbum(album)
Song song = new Song()
album.addToSong(song)
我看到的是一个新的专辑域对象,但没有新歌。我尝试了一个简单的println来检查“相册”的状态,它返回一个null。很明显我可以将一个专辑添加到现有的艺术家,但我没有收回专辑本身,所以我可以添加歌曲。有人可以了解如何在GORM / Grails中保存多个嵌套对象吗?
谢谢
Actual Domains:Nested DataLoad-> DataSeries-> ReportedResult
DATALOAD
class DataLoad {
static hasMany = [dataSeries: DataSeries]
String fileLocation
Date dateCreated
Date lastUpdated
CROSource croSource
List dataSeries = new ArrayList()
static mapping = {
dataSeries cascade:"all-delete-orphan"
}}
DataSeries
class DataSeries {
//parent and child relationships one-to-many
static belongsTo = [dataLoad: DataLoad]
static hasMany = [reportedResult: ReportedResult]
//this functions as an experiment_id for data which needs to be viewed together
String randomString
Date dateCreated
Date lastUpdated
List reportedResult = new ArrayList()
static mapping = {
reportedResult cascade:"all-delete-orphan"
}}
ReportedResult
class ReportedResult implements Serializable {
//parent and child relationships one-to-many
static belongsTo = [dataSeries: DataSeries]
static hasMany = [resultDefData: ResultDefData]
List resultDefData = new ArrayList()
//has one-to-one relationship -may need to make this a one sided relationship does not always need a Curve
Curve curve
AssayGroup assayGroup
AssayDefinition assayDefinition
AssayProtocol assayProtocol
ResultStatistic resultStatistic
ResultType resultType
//actual result fields
Float resultValue
String resultValueUnit
String resultModifier
Boolean resultObtained
Date resultTime
Integer compoundID
Float resultStatValue
String resultComment
Float resultRawValue
String linkToExternalFile
String studyNumber
Date dateCreated
Date lastUpdated
static mapping = {
resultDefData cascade:"all-delete-orphan"
}}
更新
因此,如果我尝试以下内容,它会起作用。必须是我的某个域中的其他内容。我会尝试修复我的问题,但作为参考,这应该有用。
Artist artist = new Artist(artistName: "MyName")
artist.save()
Album album = new Album(albumName: "myAlbum")
artist.addToAlbums(album)
Song song = new Song(songName: "just a song")
album.addToSongs(song)