“The [reversed ()] expression can only be applied on self-references. When an association is between two different object types, the platform will be able to determine the direction of the join automatically.”
The first statement explains that reversed() can only be used in self references. The second statement explains why reversed() is not useful for references between different object types – there, the platform can always determine the direction of the association automatically, because there is a known owner of the association.
Imagine a reference LineItem → Order
Here, when traversing the association, the direction is clear.
Now imagine a self-reference of User
Which User is the owner? Each user may have a parent and/or a child, so you need to be able to explicitly tell it which way the association should be traversed.
Hi Sameer!
IMO
We cannot use the [reversed()]
expression in the context of two different object types, just self-references.
Kindly refer https://forum.mendix.com/link/space/app-development/questions/2882 as well.
And the end part of the doc Querying-in-Reverse refers to querying specialization objects with the help of a java action to get the parent object from the specialized object.
Associations Using Self Reference in Mendix
Example: Player Entity - "Sub-type Players": Apprentice and Buddy
Association Update: Child (Owner) Updates: The association is always updated through the child. In this case, the Apprentice is the owner.
Structure of the Association
Association Name: Apprentice_Buddy
Right Side (Parent): Buddy
Left Side (Child/Owner): Apprentice
XPath Query: [SoccerSquad.Apprentice_Buddy = '[%CurrentObject%]']
Direction The XPath constraint is read from right to left.
Result: This query returns the Apprentices associated with the Buddy that is the `[%CurrentObject%]`.
Querying in Reverse
Purpose: When you have an Apprentice and want to retrieve its Buddy (Parent) from the database. Using [reversed()]: This expression tells Mendix to read the constraint in the reverse direction. Restriction: The [reversed()] expression can only be applied to self-references.
XPath Query: [SoccerSquad.Apprentice_Buddy [reversed ()] = '[%CurrentObject%]']
Objective: To find the Buddy (parent) associated with the Apprentice (child) represented by `[%CurrentObject%]`.
Reversed Direction: The [reversed()] expression reverses the query direction, making it from the perspective of the Apprentice looking for its Buddy.
Result: The query returns the Buddy associated with the current Apprentice (`[%CurrentObject%]`).