How to Retrieve Parent Entities from Child Using Associations in Java

0
Hi everyone, I'm looking for guidance on how to retrieve objects using associations in Java. Specifically, I have a relationship where the Fuga entity (child) is linked to the Hoge entity (parent) with a many-to-one association.   How can I obtain a list of Hoge entity objects from the Fuga entity side? Thanks in advance for your help!  
asked
1 answers
0

1. Retrieve the Parent (Hoge) from a Fuga object

List<IMendixObject> hogeList = Core.retrieveByPath(context, fuga, "MyFirstModule.Fuga_Hoge"); IMendixObject hoge = hogeList.isEmpty() ? null : hogeList.get(0);

  • Replace "MyFirstModule.Fuga_Hoge" with your real association name.

  • The method always returns a list, even for one-to-one or many-to-one relations.

  • If the list is empty, there’s no associated object.

 2. Retrieve All Hoges from a List of Fugas

Set<IMendixObject> uniqueHoges = new HashSet<>(); for (IMendixObject fuga : fugaList) { List<IMendixObject> hoges = Core.retrieveByPath(context, fuga, "MyFirstModule.Fuga_Hoge"); uniqueHoges.addAll(hoges); }

  • Loops through each Fuga and collects all unique Hoge objects.

  • Useful if you need all parent records linked to multiple children.

 3. Retrieve All Fugas from a Hoge (reverse direction)

List<IMendixObject> fugaList = Core.retrieveByPath(context, hoge, "MyFirstModule.Hoge_Fuga");

  • This gives you all child Fuga objects linked to a single Hoge.

answered