Search code examples
validationgrailsconstraintschildren

unsaved domain class with one to many returns child objects in random order


So here is the situation:

I have a class. For examples sake:

    class Author {
       String name

       static hasMany = [books: Book]
       static mapping = {
         books cascade: "all-delete-orphan"
       }
    }

    class Book {
       String text

       static belongsTo = [author: Author]

       static constraints = {
         text minSize: 500
       }
   }

Now, I create a form for this one to many and when I attempt to validate, the person did not enter at least 500 characters for text, so I return the object back to the form.

The problem is THE CHILD OBJECTS ARE NOT RETURNED IN THE ORDER I CREATED THEM!!! How do you all validate child objects of a domain class that has never been saved?!?!

Thanks!


Solution

  • Try this way

    class Author {
       String name
       ArrayList<Book> books = []
       static hasMany = [books: Book]
       static mapping = {
         books cascade: "all-delete-orphan"
       }
    }
    

    If not works I think you have to order by some field

    You can specify the sort order this way

    class Author {
       String name
    
       static hasMany = [books: Book]
       static mapping = {
         books cascade: "all-delete-orphan"
         books sort: 'id', order: 'desc'
       }
    
    }