JetBrains / JetBrains/Exposed

Any way to determine entity state?

Open
#1,711 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Kotlin
Stars
9.3k
Forks
798
Avg merge
4d 2h
Merged PRs (30d)
26

Description

tl;dr: Is there any way of determining whether a DAO entity is being inserted, deleted or updated in the current transaction?

Context: I am maintaining an entity change tracker that needs to detect:
- every insert (table, entity id)
- every update (table, entity id, updated column, previous and current values)
- every delete (table, entity id)

Leveraging Exposed's EntityHooks I get notified about the inserts and deletes but the updates get flushed before I am able to extract the updated values from writeValues and readValues. See [the source](https://github.com/JetBrains/Exposed/blob/6b367afc6a71d973c1cecebb0b653c60461b236d/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityLifecycleInterceptor.kt#L73-L78).

This has lead me to extract the changes by overriding the entity flush function and log the changes before forwarding to the original flush, but then I cannot differentiate between updated and inserted entities. My best bet currently is to access the internal [isNewEntity](https://github.com/JetBrains/Exposed/blob/6b367afc6a71d973c1cecebb0b653c60461b236d/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/Entity.kt#L51) function via reflection.

Hence the question: is there any recommended way I can determine entity state? Or is there some other less convoluted approach I can take to implement my change tracker?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.