{"record":{"id":"7b48a6d9f7dce686","repo":"flowable/flowable-engine","slug":"cannot-set-jpa-variable-not-configured","errorCode":null,"errorMessage":"Cannot set JPA variable:  not configured","messagePattern":"Cannot set JPA variable:  not configured","errorType":"exception","errorClass":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/JPAEntityVariableType.java","lineNumber":63,"sourceCode":"    public boolean isCachable() {\n        return forceCacheable;\n    }\n\n    @Override\n    public boolean isAbleToStore(Object value) {\n        if (value == null) {\n            return true;\n        }\n        return mappings.isJPAEntity(value);\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 != null) {\n            String className = mappings.getJPAClassString(value);\n            String idString = mappings.getJPAIdString(value);\n            valueFields.setTextValue(className);\n            valueFields.setTextValue2(idString);\n        } else {\n            valueFields.setTextValue(null);\n            valueFields.setTextValue2(null);\n        }\n    }\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/JPAEntityVariableType.java#L45-L81","documentation":"Thrown by JPAEntityVariableType.setValue when no EntityManagerSession is available in the current command context. JPA entity variables require a configured JPA persistence context so the engine can flush pending changes and read the entity's primary key.","triggerScenarios":"Setting a JPA-entity-valued process variable via runtimeService/taskService when the process engine configuration has no jpaPersistenceUnitName/jpaEntityManagerFactory configured.","commonSituations":"Forgetting to set jpaEntityManagerFactory or jpaPersistenceUnitName on SpringProcessEngineConfiguration; running engine commands outside a context that bootstrapped the EntityManager session; unit tests without JPA configuration.","solutions":["Configure jpaEntityManagerFactory (or jpaPersistenceUnitName) on the process engine configuration.","In Spring, add the <property name=\"jpaEntityManagerFactory\" .../> / EntityManagerFactory bean wiring before setting JPA variables.","If the value is not actually a managed entity, use a plain serializable type variable instead.","Catch ActivitiException and fail with an actionable configuration message."],"exampleFix":"// before\nSpringProcessEngineConfiguration cfg = new SpringProcessEngineConfiguration();\ncfg.setDataSource(dataSource); // no JPA configured\n// after\ncfg.setJpaEntityManagerFactory(entityManagerFactory);\ncfg.setJpaHandleTransaction(true);\ncfg.setJpaCloseEntityManager(true);","handlingStrategy":"validation","validationCode":"if (processEngineConfiguration.getJpaEntityManagerFactory() == null && processEngineConfiguration.getJpaPersistenceUnitName() == null) throw new IllegalStateException(\"JPA not configured; cannot use JPA entity variables\");","typeGuard":null,"tryCatchPattern":"try { runtimeService.setVariable(id, \"entity\", entity); } catch (ActivitiException e) { log.error(\"JPA EntityManagerSession missing - configure jpaEntityManagerFactory\", e); }","preventionTips":["Configure jpaEntityManagerFactory or jpaPersistenceUnitName on the engine config","Keep JPA variable usage only in engines bootstrapped with Spring/JPA support","Add a startup config test asserting JPA wiring before using entity variables"],"tags":["jpa","configuration","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-14T11:17:12.474Z"}