如何按给定的日期/ ID保存addTo?

时间:2015-12-14 21:32:09

标签: grails

我需要一些关于我的API的帮助,当我在网络上时,订单保存正确,但是当它在API上时,它就错了:

def test = parseJSON.sort { a, b -> a.ID <=> b.ID } //or dateTime, will print the same
//order when I print each of them
[IDWeb:0, conductivity:0, ReportId:2, dissolvedOxygen:0, levelWater:1, ID:1, ph:0, redoxPotential:0, temperature:0]
[IDWeb:0, conductivity:0, ReportId:2, dissolvedOxygen:0, levelWater:0, ID:2, ph:0, redoxPotential:0, temperature:0]
[IDWeb:0, conductivity:0, ReportId:2, dissolvedOxygen:0, levelWater:0, ID:3, ph:0, redoxPotential:0, temperature:0]
[IDWeb:0, conductivity:0, ReportId:2, dissolvedOxygen:0, levelWater:4, ID:4, ph:0, redoxPotential:0, temperature:0]

    test.each{
        def sample = new SampleWater()
        sample.levelWater = it.levelWater
        sample.conductivity = it.conductivity
        sample.dissolvedOxygen = it.dissolvedOxygen
        sample.redoxPotential = it.redoxPotential
        sample.ph = it.ph
        sample.temperature = it.temperature
        water.addToSamples(sample)
    }
    return water

我的问题是addTo没有按顺序保存。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您必须指定要应用于“water”域类中的SampleWater列表的订单。 即:

class BlogCategory {

    static hasMany = [
        entries : BlogEntry
    ]   

    static mapping = {
        entries: sort:'dateCreated', order:'desc'
    }
}

在此示例中,将订购BlogEntry尊重dateCreated

答案 1 :(得分:0)

确保您已在List域类中将样本类型定义为Water,以便我们可以维护广告订单:

class Water {

    static hasMany = [samples: Sample]

    List<Sample> samples = []
}

class Sample {

    def levelWater
}

默认情况下,hasMany的实现属于Set类型,它不维护广告订单,但负责唯一性。

因为现在您的样本将按插入顺序保存。