If I indicate that the results of a query are to be cached, e.g.
// This example shows the execution of a Hibernate query in a Grails app,
// but the question isn't really about Grails
Author author = new Author(name: 'Charles Dickens')
def results = Book.findByAuthor(author, [cache: true])
Is it necessary for Author to implement equals()
and hashCode()
in order for the caching to be effective?
If I don't implement these methods, it appears that if I execute the code above twice, the second author object will have a different hash code to the first, and therefore the cached results will not be returned the second time the query is executed.
Update
For those unfamiliar with GORM, the query above is equivalent to the HQL
executeQuery("from Book b where b.author = ?", author)