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

  • SEARCH
  • Home
  • 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 6870877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:44:12+00:00 2026-05-27T03:44:12+00:00

I am following the example in Grails in Action. I have an issue understanding

  • 0

I am following the example in Grails in Action. I have an issue understanding how the addTo*() function works.

I have a simple domain: User, Post, Tag with the following relationships:

  • User 1-to-M Post
  • User 1-to-M Tag
  • Post M-to-M Tag

When I run the following code (first case):

1.  def user = new User(userId: 'joe', password: 'secret').save()
2.  def tagGroovy = new Tag(name: 'groovy')
3.  def tagGrails = new Tag(name: 'grails')
4.  user.addToTags(tagGroovy)              
5.  user.addToTags(tagGrails)            
6.  
7.  def groovyPost = new Post(content: 'A groovy post')
8.  user.addToPosts(groovyPost)          
9.  groovyPost.addToTags(tagGroovy)
10.         
11. User.get(1).tags.each {println it.id + " " + it.name}
12. User.get(1).posts.each {println it.id + " " + it.content + " " + it.dateCreated}

I get this:

null grails
null groovy
null A groovy post null

And if I change lines 4, 5, and 8 to:

4.  user.addToTags(tagGroovy).save(flush: true)              
5.  user.addToTags(tagGrails).save(flush: true)    
8.  user.addToPosts(groovyPost).save(flush: true)

I get this:

2 grails
1 groovy
1 A groovy post Tue Nov 22 21:00:00 WET 2011

Can someone explain why in the first case the id and dateCreated properties are not persisted?

  • 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-05-27T03:44:13+00:00Added an answer on May 27, 2026 at 3:44 am

    It’s really a hibernate issue.

    The entire object graph gets saved only when you save your user object. So they are associated with the user but not yet persisted in the database, so they have no ids.

    In your example, if you add User.save( flush: true ) before your get(1) lines, you will see that all the new posts get persisted in the database.

    Peter Ledbrook explains it a little better in this post – http://blog.springsource.org/2010/06/23/gorm-gotchas-part-1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the following example in node.js var http = require('http'); http.createServer(function(request, response) {
I'm following the racetrack example from Jason Rudolph's book at InfoQ , using grails-1.2.1.
I have tree like following example, where every leaf is object. [1] |--[2] |
In the following example, I have a custom JComponent being drawn on green background,
I'm new to Grails development. I have a domain class like this : class
The following example is from Google to create a ColumnChart using Google Charts function
Following an example with custom exception handling in Grails, I came to the following
The following example works on Mac OS X with Apache, i.e. I get the
The following example code compiles under gcc and works as I would hope. It
I was following the Grails in Action book in order to create the following

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.