aspose file tools*
The moose likes Groovy and the fly likes Can't update the object in the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Can Watch "Can New topic
Author

Can't update the object in the database

Maharaj thak
Greenhorn

Joined: Jul 29, 2010
Posts: 27
Hi All
I have written a very simple class for a Grails app ...

class AppUser{

String blocked="no"
}

And in my controller to block the user.....

def lockme={
def per=AppUser.get(session?.user?.id)
per.blocked="blocked"
try{
per.save()

println "saved user"



}catch(Exception e){
println "reason is ...${e.printStackTrace()}"


}


}



On executing the code it prints "saved user" but the changes don't show up in the database.

Could anybody help please.

Regards
Mauji Thak
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

Are you in a transaction that needs to be committed?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

If you're saving in a controller, you really should flush the changes...



Although, good practice is to put transactional code in a service. Also, if it is still not persisting your changes you should check the object for errors:



And actually, you should be propagating those errors back up to the user.



The above code assumes a lot. You should adjust for your actual code and use case.


GenRocket - Experts at Building Test Data
Maharaj thak
Greenhorn

Joined: Jul 29, 2010
Posts: 27
Hi Gregg and Jeanne
I think the when I was doing

def per=AppUser.get(session?.user?.id) it was getting the object from the session.what i did was



per.refresh() //to get the copy from the database and not session
per.blocked="blocked"
per.save()


and it worked.

Regards
Mauji thak
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

That's good that it worked. I would still recommend reading what I wrote and implementing the service part of it. If you don't, you will have problems down the road.
Maharaj thak
Greenhorn

Joined: Jul 29, 2010
Posts: 27
Hi All,
Found out what the real problem is.But first of all here's my domain class

class AppUser{

String name
String password


static constraints={
password(blank:false,size:6..20)
}


def beforeInsert = {
password = password.encodeIt()
println "password length is ${password.size()}"
}




}

Now if I try and save a user I don't get any errors(even though the beforeInsert event prints "password length is 60" which is more than what i specified in contraints).If I try and update the user and call save() on it I get errors...and the errors are all related to the size of the password.Increasing the size of the password constraint from 20 to anything more than 60 solves it.I don't know why but it works.


Regards
Mauji Thak

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't update the object in the database