{"record":{"id":"c3ce07db05608ffa","repo":"flowable/flowable-engine","slug":"cannot-set-jpa-variable","errorCode":null,"errorMessage":"Cannot set JPA variable: ","messagePattern":"Cannot set JPA variable: ","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/JPAEntityListVariableType.java","lineNumber":91,"sourceCode":"\n                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\n                        // 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.getCommandContext().getSession(EntityManagerSession.class);\n        if (entityManagerSession == null) {\n            throw new FlowableException(\"Cannot set JPA variable: \" + EntityManagerSession.class + \" not configured\");\n        } else {\n            // Before we set the value we must flush all pending changes from\n            // the entitymanager\n            // If we don't do this, in some cases the primary key will not yet\n            // be set in the object\n            // which will cause exceptions down the road.\n            entityManagerSession.flush();\n        }\n\n        if (value instanceof List<?> list && list.size() > 0) {\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","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/JPAEntityListVariableType.java#L73-L109","documentation":"JPAEntityListVariableType (and its single-entity sibling) requires an EntityManagerSession to be present in the current command context when a JPA entity is stored as a variable. If Context.getCommandContext().getSession(EntityManagerSession.class) returns null, meaning no JPA persistence context is configured for this engine, it throws this FlowableException.","triggerScenarios":"Calling setValue for a variable whose value is a JPA entity/list (e.g. taskService.setVariable(..., entity)) on an engine whose process engine configuration lacks jpaPersistenceUnitName/jpaEntityManagerFactory, so no EntityManagerSession exists.","commonSituations":"Forgetting to set jpaPersistenceUnitName or jpaEntityManagerFactory in the ProcessEngineConfiguration; setting a JPA entity variable through a secondary engine instance without JPA enabled; unit tests that build the engine config without JPA.","solutions":["Add jpaPersistenceUnitName (or jpaEntityManagerFactory) to the ProcessEngineConfiguration so an EntityManagerSession is registered","Ensure the same engine that stores the variable is the one with JPA enabled","Verify the variable value type is actually a JPA entity and that JPA variable handling is intended","For tests, configure EntityManagerSessionFactory explicitly in the engine config"],"exampleFix":"// before\nProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();\n// after\nProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()\n    .setJpaPersistenceUnitName(\"flowable-jpa-pu\");","handlingStrategy":"validation","validationCode":"boolean jpaEnabled(ProcessEngine engine) {\n    try (Command cmd = null) { /* placeholder */ return true; }\n}\n// simpler: check config before setting JPA variables\nboolean jpaConfigured = processEngineConfig.getJpaEntityManagerFactory() != null\n    || processEngineConfig.getJpaPersistenceUnitName() != null;","typeGuard":null,"tryCatchPattern":"try { runtimeService.setVariable(executionId, \"customer\", entity); } catch (FlowableException e) { if (e.getMessage().startsWith(\"Cannot set JPA variable\")) { throw new ConfigurationException(\"Enable JPA on ProcessEngineConfiguration (jpaPersistenceUnitName)\"); } throw e; }","preventionTips":["Configure jpaPersistenceUnitName or jpaEntityManagerFactory before engine startup when JPA variables are used","Add an engine smoke test that sets/reads one JPA variable","Ensure all engine instances in the app share the JPA-enabled config"],"tags":["jpa","configuration","flowable","variables"],"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-14T05:17:10.506Z"}