{"record":{"id":"7d8493b6894ada00","repo":"hibernate/hibernate-orm","slug":"entity-may-not-be-null-7d8493","errorCode":null,"errorMessage":"Entity may not be null","messagePattern":"Entity may not be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/spi/DeleteEvent.java","lineNumber":31,"sourceCode":" * @apiNote This class predates JPA, and today should\n *          really be named {@code RemoveEvent}.\n *\n * @author Steve Ebersole\n *\n * @see org.hibernate.Session#remove\n */\npublic class DeleteEvent extends AbstractSessionEvent {\n\tprivate final Object object;\n\tprivate String entityName;\n\tprivate boolean cascadeDeleteEnabled;\n\t// TODO: The removeOrphan concept is a temporary \"hack\" for HHH-6484.\n\t//       This should be removed once action/task ordering is improved.\n\tprivate boolean orphanRemovalBeforeUpdates;\n\n\tpublic DeleteEvent(@Nonnull Object object, @Nonnull EventSource source) {\n\t\tsuper(source);\n\t\tif (object == null) {\n\t\t\tthrow new IllegalArgumentException( \"Entity may not be null\" );\n\t\t}\n\t\tthis.object = object;\n\t}\n\n\tpublic DeleteEvent(@Nullable String entityName, @Nonnull Object object, @Nonnull EventSource source) {\n\t\tthis(object, source);\n\t\tthis.entityName = entityName;\n\t}\n\n\tpublic DeleteEvent(@Nullable String entityName, @Nonnull Object object, boolean cascadeDeleteEnabled, @Nonnull EventSource source) {\n\t\tthis(object, source);\n\t\tthis.entityName = entityName;\n\t\tthis.cascadeDeleteEnabled = cascadeDeleteEnabled;\n\t}\n\n\tpublic DeleteEvent(@Nullable String entityName, @Nonnull Object object, boolean cascadeDeleteEnabled,\n\t\t\tboolean orphanRemovalBeforeUpdates, @Nonnull EventSource source) {\n\t\tthis(object, source);","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/spi/DeleteEvent.java#L13-L49","documentation":"IllegalArgumentException from the DeleteEvent constructor (this(object, source) chain): session.remove(null)/session.delete(null) attempts to build a DeleteEvent with a null object, violating the @Nonnull contract, and is rejected before any listener runs. Passing null to a delete is always a caller bug — usually a null that leaked in from a failed lookup or an optional relation.","triggerScenarios":"session.delete(null) or EntityManager.remove(null); helper methods like deleteByIdHelper(entity) invoked with a null found by a query/em.find; deleting an optional association's target that was never set.","commonSituations":"Optional-entity flows where a missing row becomes null and is passed to remove; cleaning up relations where one side is legitimately absent (should be skipped, not deleted); mock-based tests passing null; JPA spec mandates IllegalArgumentException for remove(null), so Hibernate fails fast with this message.","solutions":["Null-check before calling remove/delete — a null target usually means 'nothing to delete', which is a no-op, not an error","Trace the null to its origin (find() miss, unset optional relation) and fix or explicitly skip at that point","Prefer delete-by-id patterns (e.g. Spring Data repository.deleteById or a bulk JPQL delete) that validate the id instead of the entity"],"exampleFix":"// before\nUser user = userRepository.findByIdOrNull(id);\nem.remove(user); // id absent -> user == null -> Entity may not be null\n\n// after\nOptional.ofNullable(user).ifPresent(em::remove);","handlingStrategy":"validation","validationCode":"if (user != null) {\n    em.remove(user);\n}\n// or: Optional.ofNullable(user).ifPresent(em::remove);","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Guard all remove/delete entry points with null checks — null means nothing to delete","Prefer id-based delete methods (deleteById) that validate the identifier","Mark entity parameters @NonNull and use static analysis to catch nullable flows"],"tags":["hibernate","null-check","delete","session-api","orm"],"backgroundTag":"null-argument-to-orm-session","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}