views:

110

answers:

2

Hi!

I have a groovy system configured using tomcat and Oracle 10g. I have a groovy class which defines an follows: (reduced version)

class ChangeTicket {

static constraints = {
    chngNr(nullable:false) 
}

String chngNr
}

My controller has defined a save method:

 if (changeTicketInstance.validate() && !changeTicketInstance.hasErrors() && changeTicketInstance.save()) {         
        flash.message = "changeTicket.created"
        ...
    }

As far as I know the save method calls by default the validate method in order to know if the constraints are fullfilled or not therefore the validate method call is redundant. Anyway, when the save is performed an exception will be thrown if the field chngNr is NULL. In fact the field cannot be empty (NULL) because I've defined the constraint (nullable:false).

What am I doing wrong here?

Thanks in advance,

Luis

+3  A: 

The validate call should fail if chngNr is NULL. Some databases do not consider an empty string ("") null (HSQL). If you are binding chngNr to changeTicketInstance using params from a form it is getting assigned an empty string as a value and in that case you would want your constraint to be:

chngNr(blank:false)

Also, save() wont throw an Exception unless you use save(flush:true). Hibernate queues up the changes and, unless you flush, wont throw an actual exception.

John Wagenleitner
thanks for your answer. The problem has been solved adding an extra constraint blank:false. +1
Luixv
+2  A: 

try this:

chngName(blank:false,nullable:false)

:-)

al