25051

Why are setters in Grails called twice on save?

Question:

Look at the following Grails domain class, which modifies a value within a setter, if the object is saved the first time (if it has no id):

class Idtest { String name void setName(String name) { if(!this.id) this.name = name + "TEST" else this.name = name } }

If I generate views and controller with generate-all, start the app, and enter "hello" in the generated form, "helloTESTTEST" is saved.

The save function looks like this:

def save = { def idtestInstance = new Idtest(params) if (idtestInstance.save(flush: true)) { flash.message = "${message(code: 'default.created.message', args: [message(code: 'idtest.label', default: 'Idtest'), idtestInstance.id])}" redirect(action: "show", id: idtestInstance.id) } else { render(view: "create", model: [idtestInstance: idtestInstance]) } }

Why is the setter called twice?

Answer1:

Instead of doing if(!this.id){ }

You should use beforeInsert()

<a href="http://grails.org/doc/latest/guide/GORM.html#advancedGORMFeatures" rel="nofollow">GORM Advanced Features</a>

Answer2:

I believe it is called once when you create the object and it is saved

Then it would be called again when you retrieved the object from the database.

So..

On saving to to the database the property is set, lets assume a counter = counter + 1. so now one is saved to the database.

When you retrieve the object from the database, the domain object setter will be called again thereby incrementing the counter again counter++

This is all an assumption since the there is no controller code here for us to see how you are creating and or saving the objects so dont bash me if I am completely off

Recommend

  • (Tcl/Expect) clear screen after exit
  • Scanner nextInt() and hasNextInt() problems
  • C#: Import/Export Settings into/from a File
  • jQuery: How to AJAXify WordPress Search?
  • Trying to get the char code of ENTER key
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • How can I set a binding to a Combox in a UserControl?
  • Install PHP intl extension on MacOS
  • Grails calculated field in SQL
  • Retrieve list of sent friend requests from friend_request FQL table
  • AJAX Html Editor Extender upload image appearing blank
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • req.body is undefined - nodejs
  • Accessing IRQ description array within a module and displaying action names
  • How to add date and time under each post in guestbook in google app engine
  • How to set/get protobuf's extension field in Go?
  • Cassandra Data Model
  • JSON with duplicate key names losing information when parsed
  • Trying to switch camera back to front but getting exception
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Symfony2: How to get request parameter
  • Importing jscolor library in angular 2
  • jQuery tmpl and DataLink beta
  • Akka Routing: Reply's send to router ends up as dead letters
  • Return words with double consecutive letters
  • Is there a mandatory requirement to switch app.yaml?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • python regex in pyparsing
  • How to set the response of a form post action to a iframe source?
  • Getting Messege Twice Using IMvxMessenger
  • Android Google Maps API OnLocationChanged only called once
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?