Grails
  1. Grails
  2. GRAILS-8422

Allow the specification of ids in findOrSaveWhere

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-RC2, 2.0-RC3, 2.0.1
    • Fix Version/s: 2.2-RC3
    • Component/s: Persistence
    • Labels:
    • Environment:
      Java 6.0.29, Win 7

      Description

      Trying to achieve the following:

      I have:

      class Entry {
        String value
      
        static mapping = {
          id generator:'assigned'
        }
      ...
      }
      

      Now I'd like to do the following:

      Entry entry = Entry.findOrSaveWhere(id:key, value:value);
      

      But this raises:

      org.springframework.orm.hibernate3.HibernateSystemException: ids for this   
      class must be manually assigned before calling save() 
      

      This is probably due to the fact that "id generator:'assigned'" for
      the Entry class but it is a bit strange that the id cannot be specified in the
      constructor (not sure if this holds in general). Please improve this.

      If it worked, it would really improve readability and potentially even
      avoid an additional save() which is required when doing:

      Entry entry = Entry.findOrCreateWhere(id:key, value:value)
      entry.id = key
      entry.save()
      

        Issue Links

          Activity

          Hide
          Jeff Scott Brown added a comment -

          Fat,

          I don't see any calls to findOrSaveWhere in the project you attached.

          Show
          Jeff Scott Brown added a comment - Fat, I don't see any calls to findOrSaveWhere in the project you attached.
          Hide
          Jeff Scott Brown added a comment -

          There is a service which looks like this:

          grails-app/services/com/demo/SomeService.groovy
          package com.demo
          
          class SomeService {
          
              static expose = [ 'gwt:com.demo.GWTApp' ]
          
              void serviceMethod() {
                  Entry entry = new Entry(value: 'whatever', id: 16l);
                  entry.save();
              }
          }
          

          Is that the problematic code?

          Show
          Jeff Scott Brown added a comment - There is a service which looks like this: grails-app/services/com/demo/SomeService.groovy package com.demo class SomeService { static expose = [ 'gwt:com.demo.GWTApp' ] void serviceMethod() { Entry entry = new Entry(value: 'whatever', id: 16l); entry.save(); } } Is that the problematic code?
          Hide
          Jeff Scott Brown added a comment -

          Fat,

          I can now reproduce the problem. Thanks for the info.

          Show
          Jeff Scott Brown added a comment - Fat, I can now reproduce the problem. Thanks for the info.
          Hide
          Stefan Hojer added a comment - - edited

          This bug seems to be fixed by GRAILS-1984. (Grails 2.2-RC1)

          It is sufficient to mark the id as "bindable:true" to allow assignment in constructor / findOrSaveWhere / findOrCreateWhere.

          Example:

          class Entry {
            String value
          
            static constraints = {
              id bindable: true
            }
          
            static mapping = {
              id generator: 'assigned'
            }
          }
          
          Show
          Stefan Hojer added a comment - - edited This bug seems to be fixed by GRAILS-1984 . (Grails 2.2-RC1) It is sufficient to mark the id as "bindable:true" to allow assignment in constructor / findOrSaveWhere / findOrCreateWhere. Example: class Entry { String value static constraints = { id bindable: true } static mapping = { id generator: 'assigned' } }
          Hide
          Serge P. Nekoval added a comment -

          Hmm. It is not fixed in 2.2.1, still an issue...

          Show
          Serge P. Nekoval added a comment - Hmm. It is not fixed in 2.2.1, still an issue...

            People

            • Assignee:
              Jeff Scott Brown
              Reporter:
              Fat Zopilot
            • Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Last Reviewed:

                Development