我想创建一个用于更新实体的通用代码。这是我想出的:
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。我该如何解决它?