Search code examples
hibernatevalidationgrailsgrails-orm

Grails domain validation does not work


I am using Grails 2.5.4 and it seems the validation based on constraints defined in domain object does not work. Given:

class Pet {

    String name

    static constraints = {
        name(nullable: false, blank: false)
    }
}

When I test with code like the following:

    Pet p = new Pet()
    if(!p.save()) {
        p.errors.each {
            println it
        }
    }

I expect to see the validation output on the console. Instead, I receive runtime exception on save():

Class
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
Message
Column 'name' cannot be null

This is not the behavior I would expect. save() is not supposed to make the MySQL insertion call when the validation does not pass. Does anyone know what the problem is?

I also tested with the following:

    Pet e = new Pet()
    e.validate()
    println e.hasErrors()  

And I get the output false, which is incorrect.

The insertion works fine when I give pet a name. So the setup seems okay. Just the validation does not seem to work.


Solution

  • The problem turns out to be having hibernate plugin dependency of version 5.0.4

    It seems that version does not recognize grails 2.x domain classes.

    I upgraded to 5.0.8 and the issue was resolved.