2012-05-29 28 views
0

我想創建更新實體的通用代碼。這是我想出:合併後爲什麼實體id增加? [Grails]

def update(id, Object updatedEntity) { 
    def entity = findById(id) 
    if (entity) { 
     updatedEntity.id = id 
     def mergedEntity = updatedEntity.merge() 
     if (mergedEntity) { 
      return mergedEntity 
     } else { 
      throw new ValidationException("Cannot update.", mergedEntity.errors) 
     } 
    } else { 
     throw new IllegalArgumentException("No ${domainClass.getSimpleName()} with id=${id} found.") 
    } 
} 

爲什麼mergedEntity總是遞增id值(我的意思是,如果updatedEntity.id = 1,然後mergedEntity.id將2.我怎樣才能解決這個問題

+1

更新實體是獨立實體?我認爲你需要閱讀什麼合併實際用於。 http://grails.org/doc/latest/ref/Domain%20Classes/merge.html – Gregg

+0

Grails是一種動態語言,已經有很多內置的「通用代碼」來處理這樣的事情。你能解釋你想做什麼,爲什麼? –

+0

@Gregg:是,updatedEntity已分離。 – Roman

回答

0

我發現是什麼原因造成?描述的行爲:我id參數始終是String的實例,所以當我通過ID = 1,值實際ID爲「1」,它不被視爲休眠id

除此之外,爲了使這項工作我不得不覆蓋version財產。

相關問題