• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't update the object in the database

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you in a transaction that needs to be committed?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maharaj thak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic