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: |
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