0

Is there a way to create a query in a grails domain that always returns the records that have a specific criteria?

For example:

Class Person {

  String firstname
  String lastname
}

Now instead of saying Person.findByFirstname("Bart") all over the codebase is there anything I can do inside the Person domain class so that I can simply say something like Person.bart

birdy
  • 9,286
  • 24
  • 107
  • 171

1 Answers1

2

The namedQueries support available within Grails/GORM should meet this need: http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html

   static namedQueries = {
       firstNameBart {
           eq 'firstname', 'Bart'
       }
   }

Then used in this way:

def barts = Person.firstNameBart.list()
Andrew
  • 2,239
  • 13
  • 7
  • but my query is in hql. Can I have hql inside namedqueries? – birdy Feb 06 '13 at 20:01
  • Unfortunately, I believe `namedQueries` supports criteria builder only. If you have your HQL in an external `hbm.xml` file, you can use this method: http://stackoverflow.com/questions/8824465/using-hibernate-hql-named-queries-in-grails – Andrew Feb 06 '13 at 20:08