Owens answer got me out of the confusion i was in. i was trying to the the ordering defined on a relationship between users (1) and posts (many), but when i wrote the initial tests they were failing as using User.get (u.id) was in the same session - and so was just reading out of the cache and they came back in the order i'd written tem not newest first as i'd expected.
I then rewrote the test across two sessions and low and behold in the second session this time returned the posts in desc order.
So you just have to be careful. if you are in the same original session that creates the posts then you have to use User.get (u.id).posts.sort().
All these little gotchas with not understanding properly how the session cache and underlying DB work in the scope of the same session/transaction. makes your brain ache sometimes.
whilst we are noting things - this errored in integration test in 3.2.5, but i spotted a thread by Jeff and the fix that had gone. So i upgraded to grails 3.2.6 last night and this test now works
void "test query"() {
given:"a user and where query for users, and posts "
User u
when: "create a post for user "
User.withNewSession { session ->
u = new User(username: 'will')
u.save(flush: true, failOnError: true)
Post p1 = new Post(comment: [food: "bought coffee and cake "])
Post p2 = new Post(comment: [dinner: "bought wine and dinner"])
Post p3 = new Post(comment: [view: "spectacular view of lake and sunset"])
u.addToPosts(p1)
u.addToPosts(p2)
u.addToPosts(p3)
u.save(flush: true)
if (u.hasErrors())
println "error saving posts on user u : ${u.errors}"
def postList = User.get(u.id).posts
postList.each { println "query via user.list using same session > $it.dateCreated : $it.comment" }
Post.findAll().each { println "query via Post using same session > $it.dateCreated : $it.comment" }
}
//because still in same session it just returns the order from the 1st level cache - so force a
//new session and let the DB do the sort
def lookupPosts
User.withNewSession{ session ->
User uNew = User.get(1)
assert uNew
lookupPosts = uNew.posts
lookupPosts.each {println "query via user in new session > $it.dateCreated : $it.comment" }
}
then: " check post was added"
!u.hasErrors ()
lookupPosts.size() == 3
lookupPosts[1].comment.dinner == "bought wine and dinner"
}