{"record":{"id":"1d51f4a21aae1130","repo":"flowable/flowable-engine","slug":"exception-occurred-while-getting-value-from-id-fie","errorCode":null,"errorMessage":"Exception occurred while getting value from id field/method on JPAEntity: ${cause}","messagePattern":"Exception occurred while getting value from id field/method on JPAEntity: (.+?)","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/JPAEntityMappings.java","lineNumber":102,"sourceCode":"            throw new FlowableIllegalArgumentException(\"Object is not a JPA Entity: class='\" + value.getClass() + \"', \" + value);\n        }\n        Object idValue = getIdValue(value, metaData);\n        return getIdString(idValue);\n    }\n\n    public Object getIdValue(Object value, EntityMetaData metaData) {\n        try {\n            if (metaData.getIdMethod() != null) {\n                return metaData.getIdMethod().invoke(value);\n            } else if (metaData.getIdField() != null) {\n                return metaData.getIdField().get(value);\n            }\n        } catch (IllegalArgumentException iae) {\n            throw new FlowableException(\"Illegal argument exception when getting value from id method/field on JPAEntity\", iae);\n        } catch (IllegalAccessException iae) {\n            throw new FlowableException(\"Cannot access id method/field for JPA Entity\", iae);\n        } catch (InvocationTargetException ite) {\n            throw new FlowableException(\"Exception occurred while getting value from id field/method on JPAEntity: \" + ite.getCause().getMessage(), ite.getCause());\n        }\n\n        // Fall trough when no method and field is set\n        throw new FlowableException(\"Cannot get id from JPA Entity, no id method/field set\");\n    }\n\n    public Object getJPAEntity(String className, String idString) {\n        Class<?> entityClass = null;\n        entityClass = ReflectUtil.loadClass(className);\n\n        EntityMetaData metaData = getEntityMetaData(entityClass);\n\n        // Create primary key of right type\n        Object primaryKey = createId(metaData, idString);\n        return findEntity(entityClass, primaryKey);\n    }\n\n    private Object findEntity(Class<?> entityClass, Object primaryKey) {","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-variable-service/src/main/java/org/flowable/variable/service/impl/types/JPAEntityMappings.java#L84-L120","documentation":"Reading a JPA entity variable's id reflectively caused the invoked getter method (or field accessor path) to itself throw; reflection wraps that in InvocationTargetException. Flowable unwraps it and rethrows the underlying cause inside a FlowableException whose message includes the cause's message.","triggerScenarios":"getIdValue() invokes metaData.getIdMethod().invoke(value) and the entity's @Id getter throws any RuntimeException/Error (e.g. lazy-initialization LazyInitializationException, NullPointerException inside the getter, database access from getter).","commonSituations":"Accessing an uninitialized lazy proxy/attribute outside a Hibernate session when Flowable reads the id to serialize the variable; getter with custom logic that depends on session or transient state; entity modified to add logic in the getter that fails for detached instances.","solutions":["Read the exception cause (ite.getCause()) — fix the real failure inside the entity's id getter, not the Flowable wrapper.","Use field-based @Id access (no getter logic) or a plain getter that just returns the field without lazy logic.","Ensure the EntityManager/session is still open when the variable value is accessed (JPA entity variable types configured with proper session handling, e.g. jpaHandleTransaction=true).","Initialize the id before the entity becomes detached (id assigned at persist time, e.g. with @GeneratedValue and a flush)."],"exampleFix":"// before\n@Id\n@GeneratedValue\nprivate Long id;\npublic Long getId() { return session.load(...).getId(); } // throws when detached\n// after\n@Id\n@GeneratedValue\nprivate Long id;\npublic Long getId() { return this.id; } // plain field access","handlingStrategy":"try-catch","validationCode":"// check the getter does not depend on an open session before detaching\nif (entityManager.contains(entity)) { /* still managed, safe to read id */ }","typeGuard":null,"tryCatchPattern":"try {\n    Object v = taskService.getVariable(taskId, \"jpaVar\");\n} catch (org.flowable.common.engine.api.FlowableException e) {\n    Throwable cause = e.getCause();\n    if (cause instanceof org.hibernate.LazyInitializationException) {\n        // reopen session / reattach entity and retry\n    }\n}","preventionTips":["Keep id getters trivial (return the field, no lazy logic).","Use field-based @Id access to avoid executing getter code.","Flush after persist so the id is assigned before the entity is stored as a variable.","Keep the EntityManager session open during variable access (jpaHandleTransaction=true)."],"tags":["jpa","reflection","invocation-target","lazy-initialization"],"backgroundTag":"invalid-state-transition","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"}