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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:13:57+00:00 2026-05-28T08:13:57+00:00

I have field int displayOrder = 1 in Grails domain object and would like

  • 0

I have field int displayOrder = 1 in Grails domain object and would like to increment it by 1 each time the domain object gets saved.

If I have domain object Widget that has one-to-many relationship with Pix, every time Widget gets a new Pix, Pix needs to have displayOrder incremented.

What would be the most prudent way of setting this up? Preferably count should start from 1 to keep confusion to minimum when someone wants to change the display order. I thought about just duplicating the ID, but then we are possibly looking at displayOrder of 1123, followed by 1169 , etc., so that’s not a practical option.

The collection of Pixes belonging to Widget is a sortedSet.

  • 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-28T08:13:58+00:00Added an answer on May 28, 2026 at 8:13 am

    You could also do a Named Query to get the last max value of the Pixes, which would work in scenarios where the display orders have skips and jumps.

    class Widget {
        static hasMany = [pixes: Pix]
        ...
    }
    class Pix {
        static belongsTo = [widget: Widget]
    
        static namedQueries = {
            selectMaxDisplayOrderForWidget { widgetInstance ->
                eq('widget', widgetInstance)
                projections {
                    max('displayOrder')
                }
                uniqueResult = true
            }
        }
        ...
    }
    

    Then:

    def widget = retrieveOrCreateWidget()
    def pix = new Pix(propertyA: "A", ..., displayOrder: Pix.selectMaxDisplayOrderForWidget(widget).list(), widget: widget)
    

    If you want to reinitialize the display order numbers after a deleted Pix so they are sequential for a Widget, per your comment to @Jarred Olson, you could do:

    Pix.findAllByWidgetAndDisplayOrderGreaterThan(widget, deletedPixDisplayOrder, [sort:'displayOrder',order:'asc']).eachWithIndex { pixInstance, idx ->
        pixInstance.displayOrder = deletedPixDisplayOrder + idx
        pixInstance.save()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an INT field in my table, which I'd like to select as
In my class, I have field int count . I want to create a
I have a bitmasked int field in my database. Usually I manage it through
I'm newb with generics/iterators/enumerators etc. I have code, it keeps field number (int) and
I have filled List<Object1> Object 1 contain 2 fields (int id & string name)
I have field like a_b_c_d I want as the output a b c_d, this
I have an int field in my product table called product_stat which is incremented
I have a field type in a model called user which is an int
I have the following two entities: public class Field { public int FieldID {
I have a recursive method call. When any exception is thrown, I would like

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.