title: how to do save or update in Grails?
body: <p>If the record doesnt exist, it should insert.
  And if the record exists, Grails should do an update.</p>
  
  <pre><code>new MyEntity(attr1:'val1', attr2:'val2').saveOrUpdate()????
  </code></pre>
  
  <p><strong>EDIT</strong>
  I have modified my code as per your suggestions:</p>
  
  <pre><code>List&lt;NutDto&gt; res = np.parseNutData(file.inputStream);
  for(NutDto cellValue : res){
      def nutInstance = NutData.get(cellValue.getIdd())       
      System.out.println("nutInstance = " + nutInstance);         
      if(nutInstance){
          System.out.println("Exists : " + nutInstance);
          nutInstance.foo=cellValue.getFoo()
          nutInstance.bar=cellValue.getBar()              
      }
      else{
          System.out.println("Not Exists")
          nutInstance =   new NutData(idd:cellValue.getIdd(),
                  foo:cellValue.getFoo(),
                  bar:cellValue.getBar())
      }
  
      def saveres = nutInstance.save(failOnError: true);
      println("saveres = " + saveres);
      nutInstance.errors.each { println it }
  }
  </code></pre>
  
  <p>Still its not updating. Its throwing errors.</p>
  
  <pre><code>Field error in object 'sps.NutData' on field 'idd': rejected value [123456-1234]; codes [sps.NutData.idd.unique.error.sps.NutData.idd,sps.NutData.idd.unique.error.idd,sps.NutData.idd.unique.error.java.lang.String,sps.NutData.idd.unique.error,nutData.idd.unique.error.sps.NutData.idd,nutData.idd.unique.error.idd,nutData.idd.unique.error.java.lang.String,nutData.idd.unique.error,sps.NutData.idd.unique.sps.NutData.idd,sps.NutData.idd.unique.idd,sps.NutData.idd.unique.java.lang.String,sps.NutData.idd.unique,nutData.idd.unique.sps.NutData.idd,nutData.idd.unique.idd,nutData.idd.unique.java.lang.String,nutData.idd.unique,unique.sps.NutData.idd,unique.idd,unique.java.lang.String,unique]; arguments [idd,class sps.NutData,123456-1234]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]
  </code></pre>
  
  <p>The values are not getting updated.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <pre><code>package sps    
  class NutData {
  
      String idd
      String foo
      String bar
  
  
      static constraints = {
          idd(blank:false, unique:true)
      }
  
      static mapping = {
          table 'M_NUT_DATA'
          version false
          id generator: 'assigned', name: "idd", type: 'string'
          foo column:'FOO_COL'
          bar column:'BAR_COL'        
      }
  
      String toString(){
          return idd + '_' + foo
      }
  }
  </code></pre>