Question

I don't want to update some column values in cayenne but I want to insert them . Once I insert the row I will never want to update some column values. How can I do this in cayenne. If it is hibernate in mapping file we can specify something

<column name="column1" type="string" insert="true" update="false">

How can I specify above one in cayenne mapping file. Any help on this is really appreciated.

-Narendra

Était-ce utile?

La solution

In Cayenne you can't do it declaratively (and I don't feel like that would be a very useful abstraction to add). But you can certainly achieve the same thing with just a bit of code. Override 'validateForUpdate' in your object [1] to check that a given column hasn't been changed in the current transaction:

  public void validateForUpdate(ValidationResult validationResult) {
     super.validateForUpdate(validationResult);

     ObjectStore objectStore = (ObjectStore) getObjectContext().getGraphManager();
     DataRow snapshot = objectStore.getCachedSnapshot(getObjectId());
     if(!snapshot.get("COLUMNNAME").equals(getMyProperty())) {
        validationResult.addFailure(new SimpleValidationFailure(this, "Immutable property modified");
     }
  }

[1] http://cayenne.apache.org/docs/3.1/api/org/apache/cayenne/Validating.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top