cancel
Showing results for 
Search instead for 
Did you mean: 

variableListener did not set

edword_0516
Champ in-the-making
Champ in-the-making
i m just trying to do a simple data fetch and Object -> Json bothered me for days,  here is the code:

[java]
Task task = taskService.createTaskQuery().taskCandidateOrAssigned(userId).singleResult();
           
Repair repair =  ObjectUtil.mapToObject(runtimeService.getVariables(task.getProcessInstanceId()), Repair.class);
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).includeProcessVariables().active().singleResult();

repair.setProcessInstance(processInstance);
[/java]

everything is okay up until here, however, when i try to convert the Object repair to JSON format(in order to show data in front end), the method

[java]
VariableScopeImpl.ensureVariableInstancesInitialized()
[/java]

get called, and following exception occurs:

[java]
CommandContext commandContext = Context.getCommandContext();
if(commandContext == null) {
        throw new ActivitiException("lazy loading outside command context");
}
[/java]

it is weird because when the taskQuery and processQuery get executed, this method also gets called, but did not hit this exception, and when it tries to convert to JSON format, this exception gets throw.

looks like the task.variableInstance or process.variableInstance never gets initialized

even if i do something real simple:

[java]
JsonMapper mapper = new JsonMapper();
String result = mapper.toJson(processInstance);
[/java]

the result is null where processInstance is not.

please help out
great thanks

ERROR MSG:
Could not write JSON: lazy loading outside command context (through reference chain: cn.dantin.cloud.common.util.Response["body"]->java.util.ArrayList[0]->cn.dantin.cloud.application.repair.model.Repair["processInstance"]->org.activiti.engine.impl.persistence.entity.ExecutionEntity["variableInstances"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: lazy loading outside command context (through reference chain: cn.dantin.cloud.common.util.Response["body"]->java.util.ArrayList[0]->cn.dantin.cloud.application.repair.model.Repair["processInstance"]->org.activiti.engine.impl.persistence.entity.ExecutionEntity["variableInstances"])
4 REPLIES 4

jbarrez
Star Contributor
Star Contributor
"it is weird because when the taskQuery and processQuery get executed, this method also gets called"

The reason is that that particular bit of code only can be called _during an API call_. When you call it outside, it won't work.

I'm not fully following what you're trying to do … are we talking here about json process variables or something else?
Could you wrap your example in a unit test, that would make the discussion easier.

edword_0516
Champ in-the-making
Champ in-the-making
Hi:

any thoughts on how to do this ? (convert Task to json format)
I stuck here for a week, kinda need this urgent.

Thanks a lot
appreciated

edword_0516
Champ in-the-making
Champ in-the-making
okay, here is the unit test;

<java>
Task task = taskService.createTaskQuery().taskId(id).singleResult();
Repair repair = new Repair(id, name);
assert(JSONObject.toJson(repair) != null, true);
repair.setTask(task);
assert(JSONObject.toJson(repair) == null, true);
</java>

both test will pass, what i m trying to do is just convert Task to JSON format, and for some reason, exception occurs.

Any idea what is going on here? should i switch to another json tool? (i m using com.fasterxml.jackson)

thanks for the response btw.

trademak
Star Contributor
Star Contributor
This is not really what Joram was referring to as a unit test. Could you create a unit test that we can try as well (so a unit test project) ?

Best regards,
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.