3

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 マップに置き換えます。

4

2 に答える 2

3
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( 
                               userInstance, startDate, endDate )
ArrayList allShifts = new ArrayList()
def size = shiftRecords.size()

for ( int i = 0; i < size; i++ ){
    LinkedHashMap thisShift = new LinkedHashMap()
    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 << thisShift
}

を繰り返すたびに、新しいマップを作成する必要がありますshiftRecords。ただし、上記のコードは以下のように groovy で過度に単純化できます。

def shiftRecords = Shift.findAllByUserAndStartTimeBetween( 
                               userInstance, startDate, endDate )
def allShifts = []

shiftRecords.each{
    def thisShift = [:]
    thisShift.id = it.id
    thisShift.user = it.user
    thisShift.startTime = it.startTime
    thisShift.posCode = it.posCode
    thisShift.deptCode = it.deptCode
    thisShift.billingIDX = it.billingIDX

    Position thisPos = Position.findByPositionCode( thisShift.posCode )
    thisShift.posTitle = thisPos.shortTitle
    thisShift.deptTitle = thisPos.departmentTitle

    allShifts << thisShift
}
于 2013-09-09T21:38:16.830 に答える
3

試す:

def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
def allShifts = shiftRecords.collect { it ->
    def pos = Position.findByPositionCode( it.posCode )
    [ id         : it.id,
      user       : it.user,
      startTime  : it.startTime,
      posCode    : it.posCode,
      deptCode   : it.deptCode,
      billingIDX : it.billingIDX,
      posTitle   : pos.shortTitle,
      deptTitle  : pos.departmentTitle ]
}
于 2013-09-09T21:40:08.313 に答える