Grails
  1. Grails
  2. GRAILS-1984

id initialization on constructor when generator is assigned

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2-RC1
    • Component/s: Persistence
    • Labels:
      None

      Description

      Apparently domain classes ignore a parameter named id on the constructor, which is very reasonable in most cases. However, when the id mapping is set to generator:'assigned', this can get be a bother since one has to pass all pass all parameters and then manually assign the id. An example class would be:

      class CurrencyRow {
      // ISO Alphabetic code
      String id
      String name
      int numericCode

      static mapping =

      { id generator:'assigned', params:[type:'string'] }

      static constraints =

      { id(size:1..5) name(size:2..20, unique:true) numericCode(unique:true) }

      }

      In order to save it, one must do:

      def c = new CurrencyRow(name:'one', numericCode:10)
      c.id = 'CUR'
      c.save()

      Otherwise an exception about the id being unassigned is raised.

        Issue Links

          Activity

          Hide
          Sudheer Eega added a comment -

          Try this way
          def v = new CurrencyRow(numericCode: 100)
          v.id="test"
          or
          def v = new CurrencyRow()
          v.id="test"
          v.numericCode=100

          Show
          Sudheer Eega added a comment - Try this way def v = new CurrencyRow(numericCode: 100) v.id="test" or def v = new CurrencyRow() v.id="test" v.numericCode=100
          Hide
          Ales Bukovsky added a comment -

          This is still an issue in 2.2.0. The only way to get the domain object created/stored successfully is to assign id separately from the constructor.

          Show
          Ales Bukovsky added a comment - This is still an issue in 2.2.0. The only way to get the domain object created/stored successfully is to assign id separately from the constructor.
          Hide
          Stefan Hojer added a comment -

          It works when you add the (undocumented?) constraint "bindable: true" to the id field:

          class MyDomain {
              static constraints = {
                  // allow binding of "id" attribute (e.g. in constructor or url parameters)
                  id bindable: true
              }
          }
          
          Show
          Stefan Hojer added a comment - It works when you add the (undocumented?) constraint "bindable: true" to the id field: class MyDomain { static constraints = { // allow binding of "id" attribute (e.g. in constructor or url parameters) id bindable: true } }
          Hide
          Jeff Scott Brown added a comment -

          FYI...

          The bindable constraint is documented at http://grails.org/doc/latest/ref/Constraints/bindable.html and more details about secure data binding are noted at http://grails.org/doc/latest/guide/single.html#dataBinding.

          Show
          Jeff Scott Brown added a comment - FYI... The bindable constraint is documented at http://grails.org/doc/latest/ref/Constraints/bindable.html and more details about secure data binding are noted at http://grails.org/doc/latest/guide/single.html#dataBinding .
          Hide
          Jeff Stricker added a comment -

          Passing the id via the constructor does not work in Grails 2.3.1; I suspect there's been a regression. I downloaded the example provided by @brownj, upgraded it to 2.3.1, and it fails with the following:

          Error 2013-12-09 20:40:13,805 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the
          application: ids for this class must be manually assigned before calling save(): com.demo.CurrencyRow
          
          Show
          Jeff Stricker added a comment - Passing the id via the constructor does not work in Grails 2.3.1; I suspect there's been a regression. I downloaded the example provided by @brownj, upgraded it to 2.3.1, and it fails with the following: Error 2013-12-09 20:40:13,805 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: ids for this class must be manually assigned before calling save(): com.demo.CurrencyRow

            People

            • Assignee:
              Jeff Scott Brown
              Reporter:
              Ricardo J. Méndez
            • Votes:
              12 Vote for this issue
              Watchers:
              11 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Last Reviewed:

                Development