Issue with Core.retrieveByPath in Java with system session and non persitant objects

0
I've got a problem with retrieving a non persistant object over association in Java. The usecase is the following: I created a request handler in JAVA to serve as REST service which retrieves a JSON body. I send this JSON body as a string to a microflow which, using the new JSON mapper, creates non persitant objects: a wrapper object with a 1 to many association to detail objects. In the microflow i add data to these detail objects and the wrapperobject is the mf output. In the JAVA request handler is use this enrichted detail objects to create a JSON response using a generic action, this has worked in other projects before IMetaObject metaObject = mxObject.getMetaObject(); Collection<? extends IMetaAssociation> childAssociations = metaObject.getMetaAssociationsChild(); // Loop per type of child association for this parent object for (IMetaAssociation childAssociation : childAssociations) { // Retrieve the actual child objects List<IMendixObject> childObjects = Core.retrieveByPath(context, mxObject, childAssociation.getName()); I can find the correct association to my detail objects using .getMetaAssociationsChild but i'm not able to retrieve my child objects, i just get an empty list. I also tried to hardcode the association name but no luck either. I've used this logic in other projects. In those cases the logic is used in a JAVA action from a microflow instead of in a request handler so maybe it has something to do with the context. What's working: execute the logic in a JAVA action from the process queue so with system context What's not working: execute the logic in a request handler with Core.createSystemContext. Is use the created systemcontext to execute the microflow as well. Someone has any thoughts on how to tackle this?
asked
1 answers
2

NPEs are sessions specific. Every user session has it's own isolated area in the platform cache. This results into the situation that whenever you are working with transient entities that version of the transient entity only exists in that session cache. If you want to retrieve or use that same entity in any other action you'll need to make sure to preferably use the same context, but at a minimum the same session.

Since that NPE only lives in session-cache, creating an NPE in the session from a user and retrieving it with the system session will not result in any data.

If you create an NPE in a user's context, through a Rest server, you'll need to make sure that you use the same context later on to retrieve those entities. If you'll use the original context, by simply using this.getContext() you will be able to find these entities.

If in your action you'll need elevated privileges try using the function this.getContext().getSudoContext() rather than building a new context. Doing this will keep all your actions in the same transaction and retains all built-in transaction features. You simply run without applying security (like your microflow does)


As a best-practice whenever you write a Java action always try to use the current context (this.getContext()), by using the systemContext you risk circumventing the built-in rollback and transaction behavior of the platform which can have some really annoying side-effects if you don't think it through.

answered