title: Grails: org.hibernate.TransientObjectException
body: <p>Simple Groovy\Grails code:</p>
  
  <pre><code>def start = {
          source.save()
  
          def result = getJson(hotelSite, eng + index, [:])
          parse(JSONObject.fromObject( result.json.text() ))
  
          render "OK" 
      }
  
      def parse = {JSONObject json -&gt;
          def cities = json.get("filter").cities
          println cities
          def kievHotels = getJson(hotelSite, eng + root + '/searchhotelsbycity', ['city' : 'kiev'])
          kievHotels = JSONObject.fromObject(kievHotels.json.text())
          println kievHotels
  
          kievHotels.rows.each { ht-&gt;
              HotelText hotelText = new HotelText(lang : 'en', name : ht.l_name, description : ht.description, address : ht.address)
              hotelText.save(/*flush:true*/)
              println "hotel text saved"
              Hotel hotel = new Hotel(lat : ht.lat, lon : ht.lon, source : source, sourceid : ht.id)
              hotel.addToHotelTexts(hotelText)
              //hotel.save(/*flush:true*/)
              println "hotel saved"
              ht.options.each {op-&gt;
                new HotelFeatures(lang:'en', name : op, source : source, hotel : hotel).save()
              }
              println "options saved"
              ht.photos.each {ph-&gt;
                  new HotelPhotos(hotel : hotel, photourl : ph, type : 'hotel').save()
              }
              println "photo saved"
              hotel.save()
          }
          println "THE END"
          return "THE END"
      }
  
      def getJson = {uri, path, query -&gt;
          Thread.sleep(10)
          return withHttp(uri: uri) {
              return get(path : path, query : query) { resp, json -&gt;
  
                  return ['response' : resp, 'json' : json ]
  
              }
          }
      }
  </code></pre>
  
  <p>All must be perfect but grails throws exception after the end of "start" method\closure execution(program prints "THE END", renders "OK" message and then throws exception):</p>
  
  <p><strong>org.springframework.dao.InvalidDataAccessApiUsageException</strong>: object references an unsaved transient instance - save the transient instance before flushing: <em>com.stanfy.sweethome.domains.Hotel</em>; nested exception is </p>
  
  <p><strong>org.hibernate.TransientObjectException</strong>: object references an unsaved transient instance - save the transient instance before flushing: <em>com.stanfy.sweethome.domains.Hotel</em>
      at java.lang.Thread.run(Thread.java:619)</p>
  
  <p>Caused by: <strong>org.hibernate.TransientObjectException</strong>: object references an unsaved transient instance - save the transient instance before flushing: <em>com.stanfy.sweethome.domains.Hotel</em>
      ... 1 more</p>