Mendix: how to set entity association in Java Action

In Mendix, you can set an entity association in a microflow by simply set the association attribute of the entity.

But how to do this in a Java Action?

Let’s say you have two entities, EntityA and EntityB, and EntityA has a one to many association with EntityB. The association attribute in EntityB is EntityB_EntityA. Well its quite straight forward.

Here is the code snippet:

import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.IMendixObjectMember;
import module.name.proxies.EntityA;
import module.name.proxies.EntityB;


//... your other action code

EntityB entityB = new EntityB(context);
EntityA entityA = new EntityA(context);
entityB.setEntityB_EntityA(entityA);
// when everything is done, you might also want to commit it
entityB.commit()