Repository navigation
Hibernate 7: compare two associations to unrelated classes by their identifiers, also where a part is an association - #17154
Conversation
…/h7-compare-unrelated-association-ids-8.0.x
…ir identifiers, also where a part is an association
eqProperty and neProperty of two associations whose classes are not related, such as eqProperty('left', 'right'), took
one identifier out of each side, which fails when the identifier is composite. Hibernate does not compare entities of
unrelated classes either, so the two sides are compared by their identifiers, one part for each property of a composite
identifier, as Hibernate 5 does with the columns of the two foreign keys. A part that is itself an association is
compared by the identifier of the associated entity, in turn one part at a time. An identifier of one property and an
identifier of two are not compared: the message names both classes. An association compared with a value that is not an
entity is still compared by its identifier.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A comparison of two associations with different identifier shapes throws instead of matching nothing.
| List<Expression<?>> leftParts = identifierParts(left, entityTypeOf(left)); | ||
| List<Expression<?>> rightParts = identifierParts(right, entityTypeOf(right)); | ||
| if (leftParts.size() != rightParts.size()) { | ||
| throw new IllegalArgumentException("Cannot compare [" + left.getJavaType().getName() + "] with [" + |
There was a problem hiding this comment.
One association has a composite identifier and the other has a generated identifier. Comparing them should match no rows. It throws. Compare them without throwing, and match nothing when the identifiers differ.
…number of parts are never equal Comparing an association with a composite identifier to one with a generated identifier threw an IllegalArgumentException. Identifiers of different shapes are never equal, so == matches no row and != matches the rows that have both associations, as the comparison of their foreign keys does on Hibernate 5. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Fixed: two associations whose identifiers have a different number of parts no longer throw. Such identifiers are never equal, so |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A comparison of two composite identifiers with different part types still throws.
| List<Predicate> parts = new ArrayList<>(); | ||
| for (int i = 0; i < leftParts.size(); i++) { | ||
| parts.add(negate ? criteriaBuilder.notEqual(leftParts.get(i), rightParts.get(i)) : | ||
| criteriaBuilder.equal(leftParts.get(i), rightParts.get(i))); |
There was a problem hiding this comment.
One association has a composite identifier of text and text. The other has a number and text. Comparing them should match no rows. It throws, because only the number of parts is checked. Compare the part types too, and match nothing when they differ.
…r equal are not compared Comparing two associations whose composite identifiers have the same number of parts but a text part against a number part threw, because only the number of parts was checked. Identifiers that differ in the type of a part are never equal, as identifiers with a different number of parts are, so eqProperty matches no row and neProperty matches the rows that have both associations. Numbers of different widths are compared. SiblingAssociationPropertyComparisonSpec has the table for the criteria builder and a detached criteria; 6 of its rows fail without the change. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Fixed. Two identifiers whose parts have types that are never equal are not compared, as two with a different number of parts are not: The table in
Not covered, with the reason: |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. Two numeric identifier parts that are not the same value can still compare equal.
| List<Predicate> parts = new ArrayList<>(); | ||
| for (int i = 0; i < leftParts.size(); i++) { | ||
| parts.add(negate ? criteriaBuilder.notEqual(leftParts.get(i), rightParts.get(i)) : | ||
| criteriaBuilder.equal(leftParts.get(i), rightParts.get(i))); |
There was a problem hiding this comment.
One identifier part is the long 9007199254740993. The other is the double 9007199254740992. They are not the same value. The row still matches, because the comparison rounds the long. Match nothing when the values differ.
… are refused, not compared by a rounding Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pushed a3a23ba.
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. Two numeric identifier parts that are not the same value now throw instead of matching nothing.
| boolean numbers = Number.class.isAssignableFrom(ClassUtils.resolvePrimitiveIfNecessary(left)) && | ||
| Number.class.isAssignableFrom(ClassUtils.resolvePrimitiveIfNecessary(right)); | ||
| if (numbers && floatingPoint(left) != floatingPoint(right)) { | ||
| throw new IllegalArgumentException("Cannot compare identifier parts of type " + left.getName() + " and " + |
There was a problem hiding this comment.
One identifier part is the long 9007199254740993. The other is the double 9007199254740992. They are not the same value. Comparing them throws. Match nothing for equality, and match for inequality, without throwing.
… are never equal, and are not an error Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pushed 33863aa. The long One consequence you should know: it is a rule on the types, not on the values, so a The test: |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. Two identifiers with the same leaf values are treated as different when an embedded part has a different class.
| if (attribute instanceof SingularAttribute<?, ?> singular && singular.getType() instanceof EntityType<?> associated) { | ||
| parts.addAll(identifierParts(part, associated)); | ||
| } | ||
| else { |
There was a problem hiding this comment.
Two composite identifiers have the same leaf values and the same columns. One embedded part has a different class. Equality matches nothing, and inequality matches the row. Expand the embedded part and compare the leaves.
… of their embedded parts, whatever the classes of those parts are Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pushed d0519c7. An embedded part of a composite identifier is now compared by its leaves: The test: |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A negated comparison against a null association returns the row, and the same comparison without negation drops it.
| if (!comparable(leftParts, rightParts)) { | ||
| // identifiers of different shapes are never equal: nothing matches ==, and every row that has both associations | ||
| // matches !=, a null being unknown as it is in SQL | ||
| return negate ? criteriaBuilder.and(criteriaBuilder.isNotNull(left), criteriaBuilder.isNotNull(right)) : |
There was a problem hiding this comment.
For a row with a composite-id left and a null right that targets a single-id entity, not { eqProperty('left', 'right') } returns the row, but neProperty('left', 'right') excludes it. The fallback turns an unknown SQL result into false before negation. not { neProperty('left', 'right') } also returns the null row. A null association has to compare the same way in both forms.
…ison of identifiers that are never equal, as it is when they can be compared
When the identifiers of two associations cannot be compared, equality was a constant false and inequality required both associations,
so not { eqProperty } returned a row with a null association while neProperty and not { neProperty } treated it differently.
Each part of each side is now compared with itself, which is unknown for a null, so the comparison and every negation of it leave
the row out as SQL does for two columns.
…d-paths-8.0.x' into fix/h7-compare-unrelated-association-ids-8.0.x # Conflicts: # grails-doc/src/en/guide/upgrading/upgrading80x.adoc
…L matches it, in every form of a comparison of two associations
A table over five pairs of associations, the four states of the two associations, five forms, including a negation nested in an or beside an isNull,
and the criteria builder and a detached criteria. Two older rows that required the rows with a null association from not { eqProperty }
are corrected to what the comparison of identifiers that can be compared returns.
|
Pushed the next commits, which answer the 12-04 finding: a row with a null association was matched differently by Each part of each side is now compared with itself, and the parts are joined with The class, not the sample. Two older rows required the rows with a null association from
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A null association now compares the same way in both forms. Identifier parts the database treats as equal are still treated as unequal when their Java types differ.
| // rounds a value that a double cannot hold, so two parts that are not the same value could compare equal | ||
| return floatingPoint(a) == floatingPoint(b); | ||
| } | ||
| return a.isAssignableFrom(b) || b.isAssignableFrom(a); |
There was a problem hiding this comment.
A Character identifier 'x' and a String identifier "x" compare equal in SQL, and a LocalDate and a java.sql.Date that both map to DATE do too. This check uses Java assignability, so eqProperty matches nothing and neProperty matches the equal row. Use the mapped database type. Keep the numeric precision guard for values that are not exactly the same.
…mapped types Two identifiers were taken for never equal when the Java classes of a part were not related, so a Character and a String, or a LocalDate and a java.sql.Date, which the database compares as text and as a date, matched nothing for eqProperty and matched the equal row for neProperty. A part is now compared with another when the mapped JDBC types are of one family: text, exact numbers, floating point numbers, dates and timestamps, times, booleans and binary values. An exact number and a floating point one are still not compared, as the database converts the exact one and rounds a value that a double cannot hold, and a text and a number are not compared; a part without a basic JDBC type, such as an enum or a UUID, is compared as before, by the relation of its Java classes. SiblingAssociationPropertyComparisonSpec has the 40 rows of five pairs of types (Character and String, LocalDate and java.sql.Date, Integer and Long, Long and Double, String and Long), rows with the same value and with another, eqProperty and neProperty, a criteria and a detached criteria; 8 of them fail with the previous comparison.
|
Pushed the next commit, which answers the review of 12-05 at b222b73. You are right, and it is the same decision that the earlier rounds each answered for one pair of types: whether two parts of identifiers can be equal is a question about the database, and I was answering it with the Java classes of the parts. A The rule now is the one you named: the mapped type. The table covers the class, not the two pairs of this review.
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. Identifier parts the database treats as equal are still treated as unequal when their Java types differ.
| Family leftFamily = familyOf(left); | ||
| Family rightFamily = familyOf(right); | ||
| if (leftFamily != null && rightFamily != null) { | ||
| return leftFamily == rightFamily; |
There was a problem hiding this comment.
Composite identifiers (5L, 'x') and (5.0d, 'x') compare equal in SQL. eqProperty excludes the row and neProperty includes it. The check still rejects the pair by type family. Compare the values the database compares.
Stacked on #16821, whose branch is the base of this one, so GitHub shows only this commit. It answers James's review of #16821, where two associations whose composite identifiers hold an association to unrelated classes were still compared as entities.
Problem
On Hibernate 7,
eqProperty('leftRef', 'rightRef')of two associations to unrelated classes took one identifier out of each side. Where an identifier is composite and its first part is itself an association, those parts were then compared as entities and the query failed withCannot compare left expression of type 'SpcOwnerA' with right expression ....Change
PropertyComparisonCriterionHandlercompares the two sides by their identifiers one part at a time, as Hibernate 5 does with the columns of the two foreign keys. A part that is itself an association is compared by the identifier of the associated entity, in turn one part at a time. Two identifiers with a different number of parts are not compared: the call throws anIllegalArgumentExceptionthat names both classes. An association compared with a value that is not an entity is still compared by its identifier. The call threw before, so no working code changes; the upgrade guide says so.Tests
SiblingAssociationPropertyComparisonSpechas the sibling composite and generated identifiers, the mismatch, and two rows for composite identifiers that start with an association (throughwithCriteriaand a detached criteria): both fail without the change. The Hibernate 7 module (5424 tests) passes, and so doescodeStyle.https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL