If I update a column that is part of a composite id in a Grails domain, will model.save() work?

StackOverflow https://stackoverflow.com/questions/12480050

  •  02-07-2021
  •  | 
  •  

سؤال

The domain has 4 columns part of a composite id:

id composite: ['exemptionTermCode', 'exemptionCode', 'exemptionClassification', 'exemptionPayType']

In the controller, a ugly parody of a REST service, I have this code:

else if (request.method == "PUT" && params.rowid) {
  def a = WaiverExemption.find("from WaiverExemption as e where e.exemptionRowId = ?", [params.rowid])
  if (params.exemptionPay) a.exemptionPayType = params.exemptionPay
  if (params.exemptionClassification) a.exemptionClassification = params.exemptionClassification
  if (params.exemptionCode) a.exemptionCode = params.exemptionCode
  if (params.exemptionPriority) a.exemptionPriority = parseLong(params.exemptionPriority)
  if (params.exemptionTempFlag) a.exemptionTempFlag = params.exemptionTempFlag
  a.exemptionUserId = session.licensePlate
  a.save(flush: true, failOnError: true)

I don't get any errors on the console. I can step through, and see the model object in the watch window, and I see the correct property updated. After the save() finishes, I see in the model.errors the following:

grails.validation.ValidationErrors: 0 errors

No exceptions. No crashing the debugger. Nada. Subsequent commands are all executed. However, on the Oracle side of things, the record that was updated definitely remains un-updated. It's taken me awhile (didn't know about .errors until yesterday) to get this far, and I can do inserts and deletes with the gorm methods. I can't understand why save() is failing, I'm not getting validation issues like I have in the past. I'm not updating fields in such a way that I'd have a non-unique pk, and I'd expect that to throw an error if I did.

Is there something I'm missing here?

I am using the rowid to search for the record because by the time I've updated this, I've lost part of the pk with which to look it up. Rowid is set to updateable false.

[edit] I've also tried doing a.refresh() after the save. This fails, it's unable to find a record with that key (which you'd expect, since it didn't actually run the update query.

هل كانت مفيدة؟

المحلول

I'm afraid you will have to remove the old instance and create a new one if you change a field that is part of the composite id. That's how I've solved the problem earlier.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top