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 127791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:31:58+00:00 2026-05-11T05:31:58+00:00

Is it possible to explicitly set the id of a domain object in Grails’

  • 0

Is it possible to explicitly set the id of a domain object in Grails’ Bootstrap.groovy (or anywhere, for that matter)?

I’ve tried the following:

new Foo(id: 1234, name: 'My Foo').save() 

and:

def foo = new Foo() foo.id = 1234 foo.name = 'My Foo' foo.save() 

But in both cases, when I print out the results of Foo.list() at runtime, I see that my object has been given an id of 1, or whatever the next id in the sequence is.

Edit: This is in Grails 1.0.3, and when I’m running my application in ‘dev’ with the built-in HSQL database.

Edit: chanwit has provided one good solution below. However, I was actually looking for a way to set the id without changing my domain’s id generation method. This is primarily for testing: I’d like to be able to set certain things to known id values either in my test bootstrap or setUp(), but still be able to use auto_increment or a sequence in production.

  • 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. 2026-05-11T05:31:58+00:00Added an answer on May 11, 2026 at 5:31 am

    Yes, with manually GORM mapping:

    class Foo {   String name   static mapping = {     id generator:'assigned'   } } 

    and your second snippet (not the first one) will do the job (Id won’t be assigned when passing it through constructor).

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

Sidebar

Related Questions

I'm very new to socket programming: Is it possible to explicitly set the the
Is is possible to make the following compile without: Making IFooCollection generic Explicitly implementing
Is it possible to explicitly control the culture of controls in order to set
Possible Duplicate: Explicitly assigning values to a 2D Array? sorry if this has been
How does apache handle caching of certain files, and is it possible to explicitly
Possible Duplicate: C# 'var' keyword versus explicitly defined variables EDIT: For those who are
Is it possible to write generic class with one constructor which explicitly defines type
Is it possible to detect if no option was explicitly selected in a select
In workflow 3.0-3.5 you could explicitly set the Id of a workflow you start.
Is it possible to use jquery's .animate() method to animate properties from an (explicitly

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.