« Return to Thread: Domain object retains erroneous properties when in a Service

Domain object retains erroneous properties when in a Service

by mykol :: Rate this Message:

Reply to Author | View in Thread

hi all,

i've moved a lot of GORM calls to services instead of controllers. this is due to the fact i need transactions that spans more than one domain class. unfortunately, when erroneous properties are set to the domain class, they get retained or saved. to illustrate:


domain class:

class Book
{
String title
static constraints = {
title(blank: false)
}
}

service:

service method:

def update(book)
{
if(!book.hasErrors() || book.save())
{
//do something
}
else
{
throw new RuntimeException()
}
..
..

}

controller method:

def update = {
def book = Book.get(params['id'])
book.properties = params
if(!book.hasErrors() && bookService.update(book)
        {
            ....
        }
        else
        
...
}



if the "title" i pass is blank, and invoke the update action, when i go to "show" or "browse" actions, i see that the book's title is now blank. although it did enter the "else" clause in the "update" action. but somehow, the book retained the title property inserted to it.


verified that without the service, and putting just "if(!book.hasErrors() || book.save())" to the controller, this behavior is not seen.

am i doing anything wrong?

regards,
mykol

 « Return to Thread: Domain object retains erroneous properties when in a Service