Jan
Jan

Reputation: 11

Grails: use domain method in named query

in my domain model, I have a method that does something with my data.

e.g.

class Person {

    String lastname
    String firstname

    String bigname() {
        return lastname.toUpperCase()
    }

    static namedQueries = {
        withBigname { name ->
            eq(this.bigname(), name)
        }
    }
}

I want to use this method like a property in the named query, but

this.bigname()
only throws a
java.lang.IncompatibleClassChangeError
-Exception.

Does anyone know how to use domain methods in criteria and named queries?


Update: I now tried this:

class Person {

    String lastname
    String firstname
    String bigname

    static transients = [ 'bigname' ]

    def getBigname() {
        return lastname.toUpperCase()
    }

    static namedQueries = {
        withBigname { name ->
            eq('bigname', name)
        }
    }
}

But it only results in a "could not resolve property: bigname"-exception...

Upvotes: 0

Views: 1722

Answers (4)

antonmos
antonmos

Reputation: 1036

You cannot use class methods in queries, because queries are actually translated to SQL.

You might be able to get what you need by using writing the complexity in SQL a "SQL Restriction". Search for "SQL Restriction" on http://grails.org/doc/2.0.x/guide/GORM.html

HTH

Upvotes: 1

user439828
user439828

Reputation: 29

looks like you are trying to accomplish this:

class Person {

  String lastname
  String firstname

  static namedQueries = {
      withName { name ->
          eq('lastname', name, [ignoreCase: true])
      }
  }
}

Upvotes: -1

Colin Harrington
Colin Harrington

Reputation: 4459

On a static closure, you don't have a this. You'll either need to store the bigname in the Database, do some sort of case insensitive criteria.

Upvotes: 0

amra
amra

Reputation: 16875

Try to name the method in JavaBean getter and setter notation. Rename method bigname() to getBigname().

Upvotes: 0

Related Questions