0
votes

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...

4
You are trying to query a transient field, remove the String bigname or remove the transient and getBigname method to allow querying. Read the docs, it seems you don't have a clear idea of the transient usage.Pablo Pazos

4 Answers

1
votes

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

0
votes

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

0
votes

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.

-1
votes

looks like you are trying to accomplish this:

class Person {

  String lastname
  String firstname

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