Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:50:04+00:00 2026-06-15T02:50:04+00:00

I’ve already searched about this, but still cannot figure out what I’m doing wrong.

  • 0

I’ve already searched about this, but still cannot figure out what I’m doing wrong. After calling save() the domain object id is null.

I’ve read it’ll happen if there’s a problem when saving the object, and that save(flush:true) should throw an error if that’s the case, but it’s not. Look at my code and the output:

def pic = new Picture(title:'XX', path:"XXX")
album.addToPictures(pic).save()
if(pic.validate())
   println "no errors. New id: " + pic.id
else
   println "with errors"

Output:

no errors. New id: null

And when using flush:true

def pic = new Picture(title:'XX', path:"XXX")
album.addToPictures(pic).save(flush:true)
if(pic.validate())
   println "no errors. New id: " + pic.id
else
   println "with errors"

Output:

no errors. New id: 17

As you can see, there aren’t any errors creating the object, and I should be able to get the id of the object after just calling save(). Any ideas?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:50:07+00:00Added an answer on June 15, 2026 at 2:50 am

    You are misunderstanding the timing of when an object actually gets persisted to the database. An object does not get persisted when you call obj.save(), it gets persisted when whichever of the following happens first:

    • the transaction in which save() is called is committed
    • the Hibernate session in which save() is called is closed

    A transaction can be started explicitly with

    SomeDomainClass.withTransaction {
      // code in here runs within a transaction
    }
    

    Normally, a transaction is also implicitly started for each call to a service method

    class MyService {
    
      void doSomething () {
        // code in here runs within a transaction
      }  
    }
    

    If you don’t explicitly or implicitly use transactions, saved objects get persisted when the Hibernate session closes, which is (roughly) when the HTTP request completes.

    However, if you call someObject.save(flush: true) you are telling Hibernate to persist the object immediately, which is why

    album.addToPictures(pic).save(flush: true)
    

    assigns an ID to the Picture instance, but

    album.addToPictures(pic).save()
    

    will only assign the ID when the enclosing session/transaction is closed/committed

    Update

    Futher to your comment

    The problem is that I want to use the id as part of the name of a file I need to save. What about if I get an error saving the file? Should I use a explicit transaction and roll it back?

    Yes, use an explicit transaction, and save the file once you’re sure the object has been successfully persisted, roll the transaction back if persistence fails

    def pic = new Picture(title:'XX', path:"XXX")
    
    Picture.withTransaction { TransactionStatus status ->        
    
      try {
        album.addToPictures(pic).save()
    
      } catch(ex) {
        status.setRollbackOnly()
        throw ex
      }
    }
    
    // At this point you can be sure pic has been persisted, so use pic.id to save the file
    

    Update 2

    Further to your comment

    I don’t want to save the file once I’m sure the object has been successfully persisted, but the opposite, I want to persist the object once the file has been successfully saved. So, I’m going to reformulate my questions as “Is there a way to configure Grails so that I can know the id that’s going to be assigned to the new object before the object is effectively saved in the database?”

    You already know that

    album.addToPictures(pic).save(flush:true)
    

    will provide you with the ID of the Picture instance, so if you do this within a transaction you can get the ID without actually committing the transaction. However, I think this will only work if you’re using a database that uses sequences (Oracle, Postgres). Something like the following should work

    Picture.withTransaction { TransactionStatus status ->        
    
      try {
        def pic = new Picture(title:'XX', path:"XXX")  
        album.addToPictures(pic).save(flush: true)
    
        // pic.id should now be assigned, so save the file. I'm assuming an
        // an exception will be thrown if saving the file fails
    
      } catch(ex) {
        // you may also want to try rolling back the file save here, i.e. delete it
        status.setRollbackOnly()
        throw ex
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.