{"record":{"id":"8947c3c1d446e5e7","repo":"flowable/flowable-engine","slug":"cannot-set-jpa-variable-entitymanagersession","errorCode":null,"errorMessage":"Cannot set JPA variable: \" + EntityManagerSession.class + \" not configured","messagePattern":"Cannot set JPA variable: \" \\+ EntityManagerSession\\.class \\+ \" not configured","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/JPAEntityListVariableType.java","lineNumber":94,"sourceCode":"                for (Object entity : list) {\n                    canStore = entity != null && mappings.isJPAEntity(entity) && mappings.getEntityMetaData(entity.getClass()).getEntityClass().equals(entityClass);\n                    if (!canStore) {\n                        // In case the object is not a JPA entity or the class doesn't match, we can't store the list\n                        break;\n                    }\n                }\n            }\n        }\n        return canStore;\n    }\n\n    @Override\n    public void setValue(Object value, ValueFields valueFields) {\n        EntityManagerSession entityManagerSession = Context\n                .getCommandContext()\n                .getSession(EntityManagerSession.class);\n        if (entityManagerSession == null) {\n            throw new ActivitiException(\"Cannot set JPA variable: \" + EntityManagerSession.class + \" not configured\");\n        } else {\n            // Before we set the value we must flush all pending changes from the entitymanager\n            // If we don't do this, in some cases the primary key will not yet be set in the object\n            // which will cause exceptions down the road.\n            entityManagerSession.flush();\n        }\n\n        if (value instanceof List<?> && ((List<?>) value).size() > 0) {\n            List<?> list = (List<?>) value;\n            List<String> ids = new ArrayList<>();\n\n            String type = mappings.getJPAClassString(list.get(0));\n            for (Object entry : list) {\n                ids.add(mappings.getJPAIdString(entry));\n            }\n\n            // Store type in text field and the ID's as a serialized array\n            valueFields.setBytes(serializeIds(ids));","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/JPAEntityListVariableType.java#L76-L112","documentation":"Thrown by JPAEntityListVariableType.setValue() when no EntityManagerSession is registered in the CommandContext. Storing a JPA entity list as a process variable requires a configured JPA persistence provider; without it the engine cannot flush pending changes before persisting, so it aborts with ActivitiException.","triggerScenarios":"Setting a process variable that is a List of JPA entities (or JPAEntityListVariableType handles the value) while the process engine was not configured with jpaPersistenceEngine / EntityManagerSessionFactory; running with a basic engine configuration lacking the JPA session.","commonSituations":"Engine config missing jpaEntityManagerFactory / EntityManagerSessionFactory bean; calling runtimeService.setVariable(...) with an entity list on a non-JPA-configured engine; disabling JPA support but still passing entities as variables.","solutions":["Configure the engine with EntityManagerSessionFactory (processEngineConfiguration.setJpaEntityManagerFactory(...) or jpaEntityManagerFactory property)","Ensure EntityManagerSession is available in the current CommandContext (JTA/Spring integration registering the session)","Do not pass JPA entities as variables if JPA support is not configured; store IDs instead and look up entities in code","Verify the variable type resolves to JPAEntityListVariableType only intentionally"],"exampleFix":"// before\nProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); // no JPA\n// after\nconfig.setJpaEntityManagerFactory(entityManagerFactory)\n      .setJpaHandleTransaction(true)\n      .setJpaCloseEntityManager(true);","handlingStrategy":"validation","validationCode":"// guard before setting an entity-list variable\nif (processEngineConfig.getJpaEntityManagerFactory() == null) {\n    throw new IllegalStateException(\"Configure jpaEntityManagerFactory before storing JPA entities as variables\");\n}","typeGuard":"boolean jpaConfigured = Context.getCommandContext().getSession(EntityManagerSession.class) != null;","tryCatchPattern":"try {\n    runtimeService.setVariable(executionId, \"orders\", orderList);\n} catch (ActivitiException e) {\n    if (e.getMessage().contains(\"not configured\")) {\n        // fall back to storing IDs instead of entities\n    }\n    throw e;\n}","preventionTips":["Always configure EntityManagerSessionFactory when JPA variables are used","Prefer storing entity IDs and loading entities in application code","Add an engine-config startup check for JPA variable usage"],"tags":["jpa","configuration","variables","activiti"],"backgroundTag":"missing-required-config","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}