GORM 以与添加行相反的顺序添加行

GORM adding rows in an order that is the reverse of the order they were added

class Dog {
    String name
    String type

    static hasMany = [snacks:Snack]
}


class Snack {
    String name;
    String protein;
    Boolean vegetarian;     
}

dogInstance.addToSnacks(new Snack(name: "Dog Yums", protein: "Pork", vegetarian: false);
dogInstance.addToSnacks(new Snack(name: "Super Tasty Snack", protein: "Beef", vegetarian: false);
dogInstance.addToSnacks(new Snack(name: "Woofles", protein: null, vegetarian: true);
dogInstance.save(flush: true)

我希望零食数据库 table 的零食条目按添加顺序排列。相反,它们最终在数据库中被逆转。知道为什么会这样吗?我正在使用 MSSQL 数据库来存储数据。

我除外

id: 1, name: Dog Yums, protein: Pork, vegetarian: False

id: 2, name: Super Tasty Snack, protein: Beef, vegetarian: False

id: 3, name: Dog Yums, protein: null, vegetarian: True

我得到了什么

id: 1, name: Dog Yums, protein: null, vegetarian: True

id: 2, name: Super Tasty Snack, protein: Beef, vegetarian: False

id: 3, name: Dog Yums, protein: Pork, vegetarian: False

默认情况下,GORM(实际上是 Hibernate)不保证集合的顺序。如果您需要集合按特定顺序排列,则需要将此集合指定为 List<T>,例如:

class Dog {
    String name
    String type
    List<Snack> snacks

    static hasMany = [snacks:Snack]
}