Skip to content

FEATURE: Disable overwriting generated properties at transaction level#2943

Closed
rPraml wants to merge 1 commit into
ebean-orm:masterfrom
FOCONIS:no-overwite-generated-props
Closed

FEATURE: Disable overwriting generated properties at transaction level#2943
rPraml wants to merge 1 commit into
ebean-orm:masterfrom
FOCONIS:no-overwite-generated-props

Conversation

@rPraml

@rPraml rPraml commented Jan 23, 2023

Copy link
Copy Markdown
Contributor

We want to backup & restore some data by exporting it to JSON. For this use case we need to preserve

  • whenCreated
  • whoCreated
  • whenModified
  • whoModified

This PR allows to disable the update of these properties for a particular transaction

@rPraml rPraml force-pushed the no-overwite-generated-props branch from 2ff97fc to effc0da Compare January 23, 2023 15:31
@rbygrave

rbygrave commented Feb 1, 2023

Copy link
Copy Markdown
Member

I like the feature.

When I first read it I expect the opposite meaning for overwriteGeneratedProperties = true ... which to me means I the developer get to override the generated properties. I want to set it to true to enable me the developer to override the values of the generated properties. Currently in the PR it has the opposite meaning.

I'll ponder this ...

@rPraml

rPraml commented Feb 2, 2023

Copy link
Copy Markdown
Contributor Author

That depends on which side you look at it from:

  • do the developer want to overwrite the generated propertes (and ebean shouldn't set them back)
  • or should NOT ebean overwrite previously changed propertes.

I agree, after your comment, the name is not the best. I'm sure we'll find an other one

  • noUpdateGenerated (butIfTheyAreNullSetThem) - also a point of view problem
  • disableGeneratedUpdate
  • ...

@rPraml rPraml changed the title NEW: Feature to disable overwriting generated properties FEATURE: Disable overwriting generated properties at transaction level Aug 8, 2023
@jnehlmeier

Copy link
Copy Markdown

Is it allowed to toggle that switch on/off during the lifespan of the transaction or would that miserably cause problems? If it can be toggled on/off at any time then a setter name seems fine, e.g. setAutoUpdateGeneratedFields(boolean). If we can not toggle the setting at any time then just disableAutoUpdateGeneratedFields() to disable the default setting.

Side note: It would be so cool if that whole generated fields thing would be extendible/configurable. For example I like Who/WhenDeleted as well, because soft-deletion is not a modification of data.

@rPraml

rPraml commented Aug 8, 2023

Copy link
Copy Markdown
Contributor Author

Generated properties are updated during save, so it is possible to modify it at nearly any time
(you might not change it in a persist callback or in an other thread).

setAutoUpdateGeneratedFields(boolean)

I like that name

rPraml added a commit to FOCONIS/ebean that referenced this pull request Aug 10, 2023
@rbygrave

Copy link
Copy Markdown
Member

Replaced by #3799

@rbygrave rbygrave closed this Jun 26, 2026
rbygrave added a commit that referenced this pull request Jun 26, 2026
Adds transaction-level control over whether Ebean auto-generates values for @WhenCreated, @WhenModified, @Whocreated and @WhoModified properties.

Motivation

Backup/restore scenarios need to preserve the original audit timestamps and user values when re-inserting exported data. Without this, every save overwrites those fields with the current time/user.

Usage

 try (Transaction txn = DB.beginTransaction()) {
   txn.setGeneratedPropertiesEnabled(false);
   bean.setWhenCreated(originalTimestamp);
   bean.setWhenModified(originalTimestamp);
   DB.save(bean);
   txn.commit();
 }

Behaviour

 - Disabled (false): generated property values are only written if the property currently has a null value. Any non-null value set on the bean is preserved.
 - @Version is unaffected: the version property always auto-increments regardless of this setting, preserving optimistic locking integrity.
 - Default is true: all existing behaviour is unchanged.

Files changed

 - Transaction — new setGeneratedPropertiesEnabled(boolean) with javadoc
 - SpiTransaction — new isGeneratedPropertiesEnabled()
 - SpiTransactionProxy — delegates both methods
 - JdbcTransaction — field + implementation (default true)
 - NoTransaction, ImplicitReadOnlyTransaction — no-op setter, true getter
 - PersistRequestBean — onInsertGeneratedProperties, onUpdateGeneratedProperties, onFailedUpdateUndoGeneratedProperties all gate on isGeneratedPropertiesEnabled()
 - TestGeneratedProperties — 3 new tests covering insert-preserves, insert-null-still-filled, and update-preserves

Supersedes

PR #2943 — same feature, renamed from setOverwriteGeneratedProperties to setGeneratedPropertiesEnabled for a clearer, positive-sense API.

Co-authored-by: robin.bygrave <robin.bygrave@eroad.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants