Get association multiplicity at runtime in Java

3
Hi All, Is there any way to determine the multiplicity of entity associations at runtime from within Java code? E.g., for the domain model below, the code should be able to determine that there are 4 associations linking the Manager and Subordinate entity, and should be able to determine whether an entity refers to a collection or a singleton of the associated entity. https://gyazo.com/492650cd94fa370f300be4cfaca697b0   Use case: Given an entity fully qualified name at runtime, I need to be able to: 1. Retrieve the entity and its associations 2. Determine the name of the associated entity and determine the multiplicity between them. Note: Since I'm not aware of the entity name at compile time, there isn't any way I can use the return type of compiled functions to distinguish between a singleton and a list.
asked
3 answers
4

The IMendixObject (the variables that you actively use in microflows and Java) don't posses the information that you are looking for. These only have the information that makes up the entity, everything that the entity owns. Adding information about additional entities and associations would only consume a lot of memory. 

 

It seems that you are looking for Meta information, the descriptive information about your domain model and entities. This does provide access to all associations regardless if the entity is parent or child of the association. See the Mendix api documentation about the IMetaObject:  https://apidocs.mendix.com/7/runtime/com/mendix/systemwideinterfaces/core/meta/IMetaObject.html

This is the same information as the Model Reflection module uses, I would recommend to take a look in that module to see how the module uses that API.

Using a function like:  Core.getMetaObject( "module.entity" ).getMetaAssociationsChild()   you get the IMetaAssociation information about all the associations of which you are the child. Based on the AssociationType (ref/refset) and ownership you can determine the cardinality.

 

When using the IMeta information pay extra attention to the result combined with inheritance, the API behaves exactly as designed (last time I checked in 7.15), but there are some complexities with some of the APIs. For example getting the 'Declared' Members will only give the current entities members, but not what is inherited. There is another API for that.
I'd suggest read the API docs and play a bit with the API functions you should be able to understand what I mean in a few minutes. 

answered
1

You can try something like this:

for (IMendixObject mxObj : Core.getAllMendixObjects()) {
	for (MendixObjectReference objRef : mxObj.getReferences(this.getContext())) {
		Core.getLogger("Example").info("Entity: " + mxObj.getType() +  " has the following reference: " + objRef.getName());
	}
	for (MendixObjectReferenceSet objRefSet : mxObj.getReferenceSets(this.getContext())) {
		Core.getLogger("Example").info("Entity: " + mxObj.getType() +  " has the following reference set: " + objRefSet.getName());
	}
}

Sample output:

If you just want a single specific entity, you can do something like:

String fullyQualifiedName = "Module.Entity";
IMendixObject mxObj = Core.instantiate(this.getContext(), fullyQualifiedName);

 

answered
0

Hello Steven,

If you're using the pre-compiled wrapper methods it will implicitly return a collection or a single object in java.

i.e. subordinate.getSubordinate_Manager will always bring back a single object or a null.

If you're using the IMendixObject's getValue() method it's a bit more complicated.

What is your use-case?

answered