added a comment - - edited
Sorry if I'm not being clear.
From my example on the mailing list, you can see my two classes and their relation:
class Parent
{
static hasMany = [children: Child]
}
class Child {
static belongsTo = [parent: Parent]
static mapping =
{
sort 'id'
}
}
I did not map my collection as a List as I was unaware I needed to in order to get insertion sort ordering for my collection. From the rest of my application's perspective, the order is correct.
e.g.:
parent.children
returns a set of child Objects that are sorted by the order they were created (or something else, ID?) I'm not sure why the order is correct as I'm not implementing comparable or overriding hashCode anywhere in my domain objects.
I can declare my collection as a List in GORM, (although I can't really have any duplicates in practice) but this seems to be a weird artifact of how the JSON DomainClassMarshaller works. Its behavior is not consistent with the CollectionMarshaller, which just serializes the collection in order.
For example, calling:
parent.children as JSON
gives me the correct order.
Why does the DomainClassMarshaller go to great lengths to determine whether a set is sorted or not, when it could just preserve input order using a List and be done with it?
You could replace all of this code:
if (referenceObject instanceof SortedMap)
{
referenceObject = new TreeMap((SortedMap) referenceObject);
}
else if (referenceObject instanceof SortedSet)
{
referenceObject = new TreeSet((SortedSet) referenceObject);
}
else if (referenceObject instanceof Set)
{
referenceObject = new HashSet((Set) referenceObject);
}
else if (referenceObject instanceof Map)
{
referenceObject = new HashMap((Map) referenceObject);
}
else if (referenceObject instanceof Collection)
{
referenceObject = new ArrayList((Collection) referenceObject);
}
with
if (referenceObject instanceof Map) {
referenceObject = new LinkedHashMap((Map)referenceObject);
} else {
referenceObject = new ArrayList((Collection) referenceObject);
}
Sets don't have order, use a List (http://grails.org/doc/latest/guide/GORM.html#sets,ListsAndMaps). This is not a bug, recommend closing. Thanks, Bobby