Fletch
Fletch

Reputation: 5219

Return full domain object from Grails / GORM projection

I would like to get a list containing the most recent book written by each of a list of given authors. I tried this:

List books = Book.withCriteria {
  inList('author', authors)
  projections {
    groupProperty('author')
    max('releaseDate')
  }
}

This seems to work but unfortunately instead of a List of Books, this returns a List of Lists, each inner list being like [author, releaseDate].

How can I get it to return a list of the relevant books?

Upvotes: 2

Views: 950

Answers (1)

Ted Naleid
Ted Naleid

Reputation: 26801

I'd use HQL for this and use a subquery, something like:

Book.executeQuery("""
     from Book as book 
     where book.id in (
         select id from Book b group by b.author order by b.releaseDate desc
     )
""")

Upvotes: 2

Related Questions