query/findAllBy 内のオブジェクトから作成されたマップをリストに入力しようとしています...ループ内の最後のマップと同じマップのリストが常に表示されます。
ブレーク ポイントを設定し、メソッドをステップ実行したところ、1) クエリから返されたデータが正しいこと、2) ループをステップ実行すると、データがマップに正しく挿入されていること、3) エラーが発生することがわかりました。マップをリストに挿入します。リストに要素を挿入するために使用したすべてのメソッド (.add、.push、<<、list[(i)] = map など) は、リスト内の以前のすべての要素を上書きしてしまいます。
助けてください。なぜこれが起こっているのかわかりません。うまくいけば、これはそこにいる誰かにとって簡単なものです.
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
ArrayList allShifts = new ArrayList()
LinkedHashMap thisShift = new LinkedHashMap()
def size = shiftRecords.size()
for ( int i = 0; i < size; i++ ){
thisShift["id"] = shiftRecords[(i)].id
thisShift["user"] = shiftRecords[(i)].user
thisShift["startTime"] = shiftRecords[(i)].startTime
thisShift["posCode"] = shiftRecords[(i)].posCode
thisShift["deptCode"] = shiftRecords[(i)].deptCode
thisShift["billingIDX"] = shiftRecords[(i)].billingIDX
Position thisPos = Position.findByPositionCode( thisShift.posCode )
thisShift["posTitle"] = thisPos.shortTitle
thisShift["deptTitle"] = thisPos.departmentTitle
allShifts.add( (i), thisShift )
}
allShifts の結果が、Shift クエリの結果から抽出された選択されたデータを含むマップのリストである必要があります。shiftRecords.each と eachWithIndex を使用してみました。この問題は、thisShift マップが allShifts に挿入される時点で、どのタイプのループでも発生します。マップの 1 つのインスタンスを挿入するだけでなく、すべてのリスト要素を現在の thisShift マップに置き換えます。