Property sorting can lead to incorrect column mappings with derived embeddable keys
Description
Activity
Gavin King April 28, 2023 at 2:39 PM
I dunno man, pissing at the bottom of rabbit holes is perhaps not so advisable if we wanna stay dry down here….
Steve Ebersole April 28, 2023 at 2:34 PM
Oh I missed the pissing contest! Do I still have time to make it?
Gavin King April 28, 2023 at 1:54 PM
As deep as the hole I’m down with enum mappings?
Steve Ebersole April 28, 2023 at 1:48 PM
After #bindImplicitColumns
we have -
The columns for PersistentClass(B).getIdentifier().getColumns()
-
Column(code)
Column(cost)
Column(id)
Column(type)
The columns for ToOne(A.b)
-
Column(b_code)
Column(b_cost)
Column(b_id)
Column(b_type)
And as I mentioned, the type details are correct.
Steve Ebersole April 28, 2023 at 1:39 PMEdited
Actually, its even worse. The problem is the coordination done in TableBinder#bindForeignKey
…
In this case, we have implicit columns derived from the target entity (specifically here its id), which triggers TableBinder#bindImplicitColumns
. #bindImplicitColumns
defines the columns properly - the order and definitions are perfect.
The trouble starts with the next step which is a call to ToOne#createForeignKey
- the very first thing it does it to try to “sort properties”, which is funny because a to-one has no properties to actually sort. But what it does is to attempt to sort the columns and set its “type index”. Based on the “original order” of the composite id. This goes off the rails in 2 ways
it messes up the already proper column order
it also messes up the “type index” references which is why we see strange type details on the key side
Simply skipping the sorting for implicit, non-aggregated keys leads to later problems however. Deep rabbit hole.
Given:
The generated DDL is:
which is very wrong in multiple ways!