Skip to content

fix: resolve duplicate properties error for multiple unidirectional OneToOne relationships to same entity#33267

Open
Gonare-22 wants to merge 1 commit intojhipster:mainfrom
Gonare-22:fix/unidirectional-one-to-one-duplicate-properties
Open

fix: resolve duplicate properties error for multiple unidirectional OneToOne relationships to same entity#33267
Gonare-22 wants to merge 1 commit intojhipster:mainfrom
Gonare-22:fix/unidirectional-one-to-one-duplicate-properties

Conversation

@Gonare-22
Copy link
Copy Markdown

Fixes #33264
Problem

Multiple unidirectional OneToOne relationships targeting the same entity could generate duplicate relationship properties.

Example:

relationship OneToOne {
Order{recipient} to Member
Order{publisher} to Member
}

This resulted in duplicate property validation errors such as:

You have duplicate properties in entity Member: order

and duplicated relationship entries on the source entity.

Root Cause

Two separate issues caused the duplication:

  1. In addOtherRelationship (generators/base-application/support/relationship.ts), SQL OneToOne relationships generated identical default back-reference names (order) for multiple relationships targeting the same entity.

  2. In setRelationshipsToEntity (lib/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts), injectedFieldInTo was unnecessarily mutated during entity processing, causing relationships to be re-included during subsequent processing passes.

Solution

  • Ensure generated back-reference names are unique by appending the relationship name when necessary.
  • Remove the unnecessary mutation of injectedFieldInTo to prevent duplicate relationship processing.

Tests

Added regression tests covering:

  • multiple unidirectional OneToOne relationships to the same entity
  • single unidirectional OneToOne relationships
  • existing bidirectional OneToOne behavior

…neToOne relationships to same entity

Fixes jhipster#33264

When multiple unidirectional OneToOne relationships point from the same
source entity to the same target entity, e.g.:

  relationship OneToOne {
    Order{recipient} to Member
    Order{publisher} to Member
  }

Two bugs caused a 'duplicate properties' error:

Bug 1 - addOtherRelationship (generators/base-application/support/relationship.ts):
When auto-generating the back-reference name for a SQL OneToOne
relationship, the function always defaulted to lowerFirst(entity.name)
(e.g. 'order') for every relationship. With two relationships both
pointing from Order to Member, Member received two injected back-
references both named 'order', triggering the duplicate property check.

Fix: Before assigning the default back-reference name, check whether
that name already exists in the target entity's relationships. If it
does, append the source relationship name as a disambiguator
(e.g. 'orderAsRecipient', 'orderAsPublisher').

Bug 2 - setRelationshipsToEntity (lib/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts):
The line:
  relationshipToConvert.injectedFieldInTo = relationshipToConvert.injectedFieldInTo ?? lowerFirst(relationshipToConvert.from);
mutated the shared JDL relationship object. Since getRelatedRelationships()
uses injectedFieldInTo to decide whether to include a relationship in the
'to' list, this mutation caused subsequent entity processing to
incorrectly re-include the relationship, resulting in duplicate entries
on the source entity (e.g. 'recipient' appearing twice on Order).

Fix: Remove the mutation entirely. The relationshipName is already
correctly derived from the extractField() call above it; the mutation
was a side-effect that served no purpose other than causing this bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

relationship OneToOne is not Working as unidirectional

1 participant