Existing Groovy DSLs
[ 236 ]
When maintaining a database, it does not matter whether we add songs to artistes or
artistes to songs—GORM will maintain both sides of the relationship:
given:
def richWoman = new Song(title:"Rich Woman")
def killingTheBlues = new Song(title:"Killing the Blues")
def jimmyPage = new Artist(name:"Jimmy Page")
def alisonKrauss = new Artist(name:"Alison Krauss")
when:
richWoman.addToArtists(jimmyPage)
richWoman.addToArtists(alisonKrauss)
jimmyPage.addToSongs(killingTheBlues)
alisonKrauss.addToSongs(killingTheBlues)
and:
jimmyPage.save()
alisonKrauss.save()
then:
["Rich Woman","Killing the Blues"] ==
jimmyPage.songs.collect { it.title }
["Rich Woman","Killing the Blues"] ==
alisonKrauss.songs.collect { it.title }
["Jimmy Page", "Alison Krauss"] ==
richWoman.artists.collect { it.name }
["Jimmy Page", "Alison Krauss"] ==
killingTheBlues.artists.collect { it.name }
We add both artistes to richWoman and then add killingTheBlues to both artistes.
We only need to save the artiste objects that are the owners of the relationships,
and all associations are preserved.
http://www.ebook3000.com