Skip to content

Hibernate 7: compare two associations to unrelated classes by their identifiers, also where a part is an association - #17154

Open
borinquenkid wants to merge 9 commits into
fix/h7-subquery-root-alias-nested-paths-8.0.xfrom
fix/h7-compare-unrelated-association-ids-8.0.x
Open

borinquenkid wants to merge 9 commits into
fix/h7-subquery-root-alias-nested-paths-8.0.xfrom
fix/h7-compare-unrelated-association-ids-8.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

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 with Cannot compare left expression of type 'SpcOwnerA' with right expression ....

Change

PropertyComparisonCriterionHandler compares 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 an IllegalArgumentException that 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

SiblingAssociationPropertyComparisonSpec has the sibling composite and generated identifiers, the mismatch, and two rows for composite identifiers that start with an association (through withCriteria and a detached criteria): both fail without the change. The Hibernate 7 module (5424 tests) passes, and so does codeStyle.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 [" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Fixed: two associations whose identifiers have a different number of parts no longer throw. Such identifiers are never equal, so eqProperty matches no row, and neProperty matches the rows that have both associations (a null stays unknown, as in SQL). SiblingAssociationPropertyComparisonSpec has the composite-id against generated-id case for eqProperty, neProperty, not { eqProperty } and a DetachedCriteria; the feature that expected the exception is replaced. The Hibernate 7 module and codeStyle pass.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@borinquenkid

Copy link
Copy Markdown
Member Author

@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: eqProperty of such associations matches no row and neProperty matches the rows that have both. A part is compared with the part in the same position of the other identifier, so text against a number fails in any position, and numbers of different widths (Long against Integer) are compared as numbers.

The table in SiblingAssociationPropertyComparisonSpec runs eight rows through the criteria builder and through a detached criteria; six of the fifteen features fail without the change:

  • Part types: a composite of text and text against number and text (your case), against text and number (the second part differs), and Long and text against Integer and text (compared, equal and different rows).
  • Operators and shape: eqProperty, neProperty, not { eqProperty }, and an or of one comparison that is made and one that is never equal.
  • Data: an owner whose two associations are set and compare equal, ones that differ, and one with only one of them set.

Not covered, with the reason: gtProperty, ltProperty and the other ordering comparisons of two associations to unrelated classes are not part of this pull request, which is eqProperty and neProperty; an association part whose identifier has a different type than the one it faces (the identifier of a part that is itself an association) goes through the same check, but I did not give it a row of its own. The upgrade note is updated, and the Hibernate 7 module, codeStyle and the repository conventions pass.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed a3a23ba.

  • A composite id of text and text against one of a number and text: this matches no row for eqProperty and every row with both associations for neProperty, and it does not throw. It was fixed in the previous push (6791a2f) and SpcTextNum and SpcMixed in SiblingAssociationPropertyComparisonSpec hold it, in the criteria builder and in a DetachedCriteria.
  • The long 9007199254740993 against the double 9007199254740992: a database converts the integral part to compare it with a floating point one, which rounds a value a double cannot hold, so the two compared equal. An integral part against a floating point one is now refused with an IllegalArgumentException that names both types, and not compared. I chose a refusal over "match nothing" because 1 and 1.0 are the same value and would then never match; the two new rows (eqProperty and neProperty of a Long pair against a Double pair) fail without the change. The 8.0.1 upgrade note says so.

grails-data-hibernate7-core runs 5,434 tests green and codeStyle is clean.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 " +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed 33863aa. The long 9007199254740993 against the double 9007199254740992 no longer throws: eqProperty matches no row and neProperty matches the rows that have both associations. An integral part and a floating point one are treated like a text part against a number, as parts whose types are never equal: comparableTypes, PropertyComparisonCriterionHandler.java line 181, the rule at line 187. This replaces the refusal of the previous push.

One consequence you should know: it is a rule on the types, not on the values, so a Long part and a Double part that hold the same value, such as 1 and 1.0, do not match either. Comparing the values would depend on how each database converts the integral number, which rounds exactly the values that are the problem here; a composite identifier with a floating point part is rare enough that I chose the rule that cannot give a wrong match.

The test: SiblingAssociationPropertyComparisonSpec line 125, in the criteria builder and in a DetachedCriteria, with a row that has no second association; it fails without the change. grails-data-hibernate7-core runs 5,433 tests green and codeStyle is clean. The upgrade note is upgrading80x.adoc line 5055. Nothing in the guide defines identifier comparison across such parts.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed d0519c7. An embedded part of a composite identifier is now compared by its leaves: PropertyComparisonCriterionHandler.addPart, line 231 expands an embedded value, at line 235, into its properties in the order of their names, as it already did for an association, so two embedded parts of different classes with the same leaves are the same columns and compare by value.

The test: SiblingAssociationPropertyComparisonSpec line 146, in a criteria builder and in a DetachedCriteria: one holder whose two identifiers have equal leaves in two classes (eqProperty matches it, neProperty does not) and one that differs in one leaf (the other way round). The entities are JPA entities with an @EmbeddedId that holds an @Embedded part (line 524 on): a GORM id composite cannot name an embedded property, the session factory does not build, so that is the form this case can take. Both rows fail without the change. grails-data-hibernate7-core runs 5,435 tests green and codeStyle is clean. Nothing in the guide defines identifier comparison across such parts.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants