{"record":{"id":"91385268d4bb5ad7","repo":"hibernate/hibernate-orm","slug":"null-object-passed-to-getcurrentlockmode","errorCode":null,"errorMessage":"null object passed to getCurrentLockMode()","messagePattern":"null object passed to getCurrentLockMode\\(\\)","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java","lineNumber":546,"sourceCode":"\t}\n\n\t/**\n\t * clear all the internal collections, just\n\t * to help the garbage collector, does not\n\t * clear anything that is needed during the\n\t * afterTransactionCompletion() phase\n\t */\n\t@Override\n\tprotected void cleanupOnClose() {\n\t\tpersistenceContext.clear();\n\t}\n\n\t@Override\n\tpublic LockMode getCurrentLockMode(Object object) {\n\t\tcheckOpen();\n\t\tcheckTransactionSyncStatus();\n\t\tif ( object == null ) {\n\t\t\tthrow new NullPointerException( \"null object passed to getCurrentLockMode()\" );\n\t\t}\n\n\t\tfinal var lazyInitializer = extractLazyInitializer( object );\n\t\tif ( lazyInitializer != null ) {\n\t\t\tobject = lazyInitializer.getImplementation( this );\n\t\t\tif ( object == null ) {\n\t\t\t\treturn LockMode.NONE;\n\t\t\t}\n\t\t}\n\n\t\tfinal var entry = getEntityEntry( object );\n\t\tif ( entry.getStatus().isDeletedOrGone() ) {\n\t\t\tthrow new ObjectDeletedException( \"Given entity was removed\", entry.getId(),\n\t\t\t\t\tentry.getPersister().getEntityName() );\n\t\t}\n\t\telse {\n\t\t\treturn entry.getLockMode();\n\t\t}","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java#L528-L564","documentation":"Session.getCurrentLockMode(object) reports the lock mode of a managed entity, but it has no meaningful answer for null — so Hibernate throws NullPointerException explicitly ('null object passed to getCurrentLockMode()') rather than letting a generic NPE surface later. After the null check the code unwraps proxies and looks up the EntityEntry, so a non-null managed object is a hard precondition.","triggerScenarios":"Calling session.getCurrentLockMode(null) — usually because a variable holding the entity is null after a failed lookup (getReference with wrong id, a get() that returned null and was not checked).","commonSituations":"Chaining operations after session.find() without a null check; passing an Optional.orElse(null) result; refactoring that renames fields and leaves the parameter unset; test code exercising lock modes with placeholder nulls.","solutions":["Null-check the entity before asking for its lock mode: if (obj == null) return LockMode.NONE / handle","Verify the entity actually loaded: use session.find() and check the result, or session.contains(obj)","Use Optional chaining: session.find(...).map(o -> session.getCurrentLockMode(o)).orElse(LockMode.NONE)","Enable -XX:+ShowHiddenFrames/parameter logging or assert inputs in debug builds to catch where null originates"],"exampleFix":"// before\nObject order = session.get(Order.class, orderId);\nLockMode mode = session.getCurrentLockMode(order); // NPE when orderId not found\n\n// after\nOrder order = session.get(Order.class, orderId);\nif (order == null) {\n    throw new EntityNotFoundException(\"Order \" + orderId + \" not found\");\n}\nLockMode mode = session.getCurrentLockMode(order);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(object, \"entity passed to getCurrentLockMode() must not be null\");\nLockMode mode = session.getCurrentLockMode(object);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check load results before use: session.find() returns null for missing ids","Avoid Optional.orElse(null) for entities flowing into session APIs","Add @NonNull/Nullness annotations (JPA/Hibernate use them) so IDEs flag nullable args at compile time"],"tags":["hibernate","session","lock-mode","null-pointer","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}