-
Notifications
You must be signed in to change notification settings - Fork 793
fix!: EXPOSED-851 Not use Cached Entity when reference column defined with .references()
#2614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
obabichevjb
wants to merge
1
commit into
main
Choose a base branch
from
obabichev/exposed-851-reference-with-entity-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ts/src/test/kotlin/org/jetbrains/exposed/v1/tests/shared/entities/EntityReferrersTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package org.jetbrains.exposed.v1.tests.shared.entities | ||
|
|
||
| import org.jetbrains.exposed.v1.core.ReferenceOption | ||
| import org.jetbrains.exposed.v1.core.dao.id.EntityID | ||
| import org.jetbrains.exposed.v1.core.dao.id.IntIdTable | ||
| import org.jetbrains.exposed.v1.core.dao.id.LongIdTable | ||
| import org.jetbrains.exposed.v1.core.eq | ||
| import org.jetbrains.exposed.v1.dao.IntEntity | ||
| import org.jetbrains.exposed.v1.dao.IntEntityClass | ||
| import org.jetbrains.exposed.v1.dao.LongEntity | ||
| import org.jetbrains.exposed.v1.dao.LongEntityClass | ||
| import org.jetbrains.exposed.v1.dao.with | ||
| import org.jetbrains.exposed.v1.jdbc.insertAndGetId | ||
| import org.jetbrains.exposed.v1.tests.DatabaseTestsBase | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class EntityReferrersTests : DatabaseTestsBase() { | ||
|
|
||
| object AlertItemTable : IntIdTable("alert_item") { | ||
| val isAlarm = bool("is_alarm").default(true) | ||
| } | ||
|
|
||
| class AlertItemEntity(id: EntityID<Int>) : IntEntity(id) { | ||
| companion object : IntEntityClass<AlertItemEntity>(AlertItemTable) | ||
|
|
||
| val bids by ItemBidEntity.referrersOn(ItemBidTable.alertItemId, cache = true) | ||
| } | ||
|
|
||
| object ItemBidTable : LongIdTable("item_bid") { | ||
| val alertItemId = integer("alert_item_id").references(AlertItemTable.id, onDelete = ReferenceOption.CASCADE) | ||
| } | ||
|
|
||
| class ItemBidEntity(id: EntityID<Long>) : LongEntity(id) { | ||
| companion object : LongEntityClass<ItemBidEntity>(ItemBidTable) | ||
| } | ||
|
|
||
| @Test | ||
| fun testCacheIsUsedWithReference() { | ||
| withTables(AlertItemTable, ItemBidTable) { | ||
| repeat(3) { | ||
| val itemId = AlertItemTable.insertAndGetId { | ||
| it[isAlarm] = true | ||
| } | ||
| repeat(5) { | ||
| ItemBidTable.insertAndGetId { | ||
| it[alertItemId] = itemId.value | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val counter = executionsCounter() | ||
|
|
||
| AlertItemEntity | ||
| .find { AlertItemTable.isAlarm eq true } | ||
| .with(AlertItemEntity::bids) | ||
|
|
||
| assertEquals(2, counter.count, "'find()' must execute exactly 2 statements. One to fetch items, another one to fetch all the bids") | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding, the current setup is intentional.
reference()andColumn.references()are not meant to be 100% equivalent or to be aligned exactly.Column.references()exists for users that want to keep the column they have defined/registered, but only additionally create a foreign key on table DDL.There could be DSL users who are choosing to use
IntIdTablefor convenience but don't want to deal withEntityIDany more than necessary and who purposefully want to strip the entityID wrapper from the referenced column.I think there could be a lot of people who want the original distinction to remain:
My question is:
If the user is relying on DAO, and therefore expects the cache to be utilised to minimise db queries, why are they using
.references()instead ofreference()?If there is no reason, maybe we should be making it clear in documentation that
.references()does not involve the entity cache?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chantal Loncle (@bog-walk) Thank you, it makes sense,
in this case it could be better update documentation,
but in this case I see the problem that Exposed allows to create reference for entity on such a column, and allows to fetch entities using
with(), but makes extra queries in this case, and there is no way for the user that it would happen without checking the logs, and it's easy to mixreference()andreferences()within code.I will check, probably it could be easy to prevent usage of
referrersOn()on columns that have noEntityID<T>in type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firstly, thank you for taking care of this issue as an issue reporter.
I didn't expect the
references()method to affect the cache behaviour either and had to look at the code to analyse the cause. As mentioned above, If thereferences()method is not intended to wrapEntityID, It would be helpful to have clear documentation stating that cached entities will not be used for reference columns withreferences().However, as obabichevjb said, even with documentation, the behavior of
references()appears fine on the surface, and the issue is only apparent in the logs. Therefore, I think it's better to be more defensive to prevent user mistakes.