Last active
December 22, 2015 21:09
-
-
Save mattfeury/6531070 to your computer and use it in GitHub Desktop.
Test case for inherited namedQuery bug in Grails
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 org.example | |
| class Book { | |
| String author | |
| static namedQueries = { | |
| withAuthor { author -> | |
| eq 'author', author | |
| } | |
| } | |
| } |
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 org.example | |
| class Encyclopedia extends Book { | |
| Integer volume | |
| } |
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 org.example | |
| class PictureBook extends Book { | |
| String pictureData | |
| static namedQueries = { | |
| withPicture { picture -> | |
| eq 'pictureData', picture | |
| } | |
| } | |
| } |
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
| // Run this script to see the two main bugs. | |
| // Create an instance of each child class | |
| def e = new Encyclopedia(volume: 3, author: "Jimmy Wales") | |
| def p = new PictureBook(pictureData: ":)", author: "Jimmy Wales") | |
| [p, e]*.save(failOnError: true) | |
| // Querying PictureBook, you would expect to see only the PictureBooks that match this query. | |
| // We actually get back both PictureBooks and Encyclopedias since we're using a namedQuery written for the Base class. | |
| assert PictureBook.withAuthor(p.author).list().size() == 1 // this is actually 2. | |
| // This will throw an error: | |
| // java.lang.NullPointerException: Cannot invoke method withPicture() on null object | |
| // It seems like the withAuthor is putting it in the context of the base class, where withPicture doesn't exist | |
| def list = PictureBook.withAuthor(p.author).withPicture(p.pictureData).list() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment