Skip to content

Instantly share code, notes, and snippets.

@mattfeury
Last active December 22, 2015 21:09
Show Gist options
  • Select an option

  • Save mattfeury/6531070 to your computer and use it in GitHub Desktop.

Select an option

Save mattfeury/6531070 to your computer and use it in GitHub Desktop.
Test case for inherited namedQuery bug in Grails
package org.example
class Book {
String author
static namedQueries = {
withAuthor { author ->
eq 'author', author
}
}
}
package org.example
class Encyclopedia extends Book {
Integer volume
}
package org.example
class PictureBook extends Book {
String pictureData
static namedQueries = {
withPicture { picture ->
eq 'pictureData', picture
}
}
}
// 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