Fletch
Fletch

Reputation: 5229

Grails best way to iterate all ids of a domain class

I have code like this:

Book.list().each {
  // real code actually does something more useful
  println "My id is " + it.id
}

It strikes me as a bit of a waste that the entire object of each Book is being loaded just to access the id. There is a load() method in Grails for when you only want to operate on the ID and I'm wondering if there's an equivalent here for loading all the domain instances? Should I use HQL? Or should I just leave it as-is?

PS: It makes me wonder whether there should be a option available to most GORM methods (finders etc) which cause it to only "load" instead of "get" the target class

Upvotes: 13

Views: 6648

Answers (4)

David Sawyer
David Sawyer

Reputation: 557

+1 For @Ruben's answer, but you could shorten it up to simply

def criteria = Book.withCriteria {
    projections {
        property 'id'
    }
}

and get the same results!

Upvotes: 5

Ruben
Ruben

Reputation: 9120

A criteria query in combination with a projection solves your problem when you want to avoid using HQL.

    def criteria = Book.createCriteria()
    def result = criteria.list {
        projections {
            property 'id'
        }
    }

The Hibernate SQL logging shows that only the IDs are loaded from the database, and not the entire Books: select this_.id as y0_ from book this_.

The criteria query can also be added as a named query on the Book domain class, providing easy access to the list of IDs.

Upvotes: 9

Tomas Lin
Tomas Lin

Reputation: 3532

You can use hql to just return the fields you need

Book.executeQuery( "select b.id from Book b" );

Upvotes: 8

Michael Legart
Michael Legart

Reputation: 820

Unless the Book object contains a lot of data, I would just go with Book.list to keep it simple.

Remember that load() does not actually hit the database, it just constructs an object with the id set.

Upvotes: -2

Related Questions