Created
August 25, 2012 21:17
-
-
Save joeRinehart/3471073 to your computer and use it in GitHub Desktop.
GrailsModelsPart3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package recordstore | |
| class Album { | |
| // Mappings | |
| static hasMany = [ | |
| songs : Song, | |
| artists : Artist | |
| ] | |
| // Properties | |
| String name | |
| List songs | |
| static constraints = { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package recordstore | |
| class Artist { | |
| String name | |
| static hasOne = [ | |
| biography : Biography | |
| ] | |
| static constraints = { | |
| biography( unique: true ) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package recordstore | |
| class Artist { | |
| String name | |
| static hasOne = [ | |
| biography : Biography | |
| ] | |
| static belongsTo = [ | |
| album : Album | |
| ] | |
| static hasMany = [ | |
| album : Album | |
| ] | |
| static constraints = { | |
| biography( unique: true ) | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| void artistHasOneBiographyShouldCascadeSaves() { | |
| def originalBiographyCount = Biography.list().size() | |
| // Create a bio | |
| def bio = new Biography( content: "Two guys and friends from Concord, NC") | |
| // Create an artist | |
| def artist = new Artist( name: "The Avett Brothers", biography : bio ) | |
| // References should be set up | |
| assert artist.biography == bio | |
| assert bio.artist == artist | |
| // Saving album should save song | |
| assert !bio.id | |
| artist.save() | |
| assert Biography.list().size() == originalBiographyCount + 1 | |
| assert bio.id | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| void artistIsManyToManyAlbum() { | |
| // Note that we save from the 'owning' side! | |
| def originalArtistCount = Artist.list().size() | |
| def album = new Album( name : "Chimes of Freedom") | |
| .addToArtists( new Artist( name : "The Avett Brothers", biography : new Biography( content: "Two guys etc." ) ) ) | |
| .addToArtists( new Artist( name : "Johnny Cash", biography : new Biography( content: "A man in 0x000000." ) ) ) | |
| .save() | |
| assert album.id | |
| assert Artist.list().size() == originalArtistCount + 2 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package recordstore | |
| class Biography { | |
| static belongsTo = [ | |
| artist : Artist | |
| ] | |
| String content | |
| static constraints = { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment