我更新了下面的问题
我试图在 Grails 中保存一系列嵌套的域对象,但我遇到了语法问题。
例如,假设我有以下嵌套的一对多关系:
艺术家->专辑->歌曲
我首先创建一个艺术家
Artist artist = new Artist(parameters)
artist.save()
对于新保存的艺术家,我希望保存一系列专辑及其相关歌曲。
我认为这样的事情会起作用,但我得到的结果好坏参半。
Album album = new Album()
artist.addToAlbum(album)
Song song = new Song()
album.addToSong(song)
我看到的是一个新的专辑域对象,但没有新的歌曲。我尝试了一个简单的 println 来检查“专辑”的状态,它返回一个空值。很明显,我可以将专辑添加到现有的艺术家,但我没有取回专辑本身,以便我可以添加歌曲。有人可以阐明如何在 GORM/Grails 中保存多个嵌套对象吗?
谢谢
实际域:嵌套 DataLoad->DataSeries->ReportedResult
数据加载
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"
}}
数据系列
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"
}}
报告结果
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)