fix: resolve entity-typed primary keys in id-based where expressions#282
Merged
Conversation
An id-based lookup (findAllById, whereId) on an entity whose primary key is itself an entity resolved the key values by type. When the key entity's table appears more than once in the root's join graph (a junction row whose second foreign key reaches the same table again), the type-based lookup is ambiguous and the query failed with "Cannot uniquely identify object in expression". The write set's AndFetch variants hit this on every fetch-back of such a junction. Object-expression resolution now falls back to the root's primary key path when the value type matches the primary key type, mirroring the existing primary-key fallback for scalar values.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
An id-based lookup (
findAllById,whereId) on an entity whose primary key is itself an entity resolved the key values by type. When the key entity's table appears more than once in the root's join graph, the type-based lookup is ambiguous and the query fails with:The canonical shape is a junction row keyed by an entity whose table is also reachable through the junction's second foreign key:
The write set's
AndFetchvariants (#269, #270) hit this on every fetch-back of such a junction:WriteSetImpl.fetch()callsfindAllByIdwith the propagated key entities, soinsertAndFetch/upsertAndFetchof an affected graph always fails. Seen in production as a batch-sync failure on every batch containing new users.Cause
whereIdbuilds anObjectExpressionwithout a metamodel, soQueryModelImpl.getMetamodelre-derives the target from the runtime value type. ForData/Refvalues it only tries root-type assignability and graph-unique type resolution before throwing; the primary-key fallback (isPrimaryKeyValue) further down is only reachable for scalar values. An entity-typed key value therefore never resolves against the primary-key path, even though the call site's contract (ID = Owner) is exact.Fix
In
getMetamodel, when type-based resolution is ambiguous, fall back to the root's primary-key path if the value type matches the root's primary-key type. This mirrors the existing scalar behavior (anIntegerid against anInteger-keyed root resolves to the primary key even when other integer columns exist) and leaves every currently-resolving case untouched; only the previously-throwing case changes. Genuinely ambiguous expressions (a non-key type appearing twice) still throw.Tests
EntityRepositoryIntegrationTest#testFindAllByIdWithEntityTypedPkWhoseTableIsReachedTwice: repository-only repro; the insert succeeds, the id-based read failed before this fix.WriteSetIntegrationTest#testInsertAndFetchJunctionWithEntityTypedPrimaryKey: write-set repro viainsertAndFetchof anOwnerPrimaryPetgraph, matching the production failure path.OwnerPrimaryPet+owner_primary_petschema (no seed rows).Both tests fail with the exact error above without the fix. Full storm-core suite: 2307 tests, 0 failures.
Relates to #269.