hibernate/hibernate-orm · error · IllegalArgumentException
Entity may not be null
Error message
Entity may not be null
What it means
Thrown by PersistenceUnitUtilImpl.getIdentifier() as an IllegalArgumentException when it is called with a null entity. PersistenceUnitUtil (jakarta.persistence.PersistenceUnitUtil) contractually requires a non-null managed entity; the implementation guards explicitly even though the parameter is annotated @Nonnull.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/jpa/internal/PersistenceUnitUtilImpl.java:88
Hibernate.initialize( entity );
}
@Override
public boolean isInstance(@Nonnull Object entity, @Nonnull Class<?> entityClass) {
return entityClass.isAssignableFrom( Hibernate.getClassLazy( entity ) );
}
@Override
@Nonnull
public <T> Class<? extends T> getClass(@Nonnull T entity) {
return Hibernate.getClassLazy( entity );
}
@Override
public Object getIdentifier(@Nonnull Object entity) {
//noinspection ConstantValue
if ( entity == null ) {
throw new IllegalArgumentException( "Entity may not be null" );
}
final var lazyInitializer = extractLazyInitializer( entity );
if ( lazyInitializer != null ) {
return lazyInitializer.getInternalIdentifier();
}
else if ( isManagedEntity( entity ) ) {
final var entityEntry = asManagedEntity( entity ).$$_hibernate_getEntityEntry();
if ( entityEntry != null ) {
return entityEntry.getId();
}
else {
return getIdentifierFromPersister( entity );
}
}
else {
return getIdentifierFromPersister( entity );
}View on GitHub (pinned to fad1729dce)
Solutions
- Null-check the entity before calling getIdentifier.
- Fix the upstream lookup that produced null (handle empty Optional from findById).
- Wrap the call in Objects.requireNonNullElse or return early when the reference is absent.
Example fix
// before Object id = emf.getPersistenceUnitUtil().getIdentifier(orderService.find(id)); // find() may return null // after Order order = orderService.find(id); if (order == null) return null; Object oid = emf.getPersistenceUnitUtil().getIdentifier(order);
Defensive patterns
Strategy: validation
Validate before calling
if (entity == null) throw new IllegalArgumentException("entity must be loaded before id extraction");
Object id = emf.getPersistenceUnitUtil().getIdentifier(entity); Type guard
Object identifierOf(EntityManagerFactory emf, @Nullable Object entity) {
return entity == null ? null : emf.getPersistenceUnitUtil().getIdentifier(entity);
} Prevention
- Resolve Optional results fully (orElseThrow/orElse) before touching PersistenceUnitUtil.
- Encapsulate getIdentifier/getVersion behind helper methods that accept null defensively.
When it happens
Trigger: Calling entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity) where entity is null - typically because a lookup returned null (findById miss, map.get miss) or an association was never initialized.
Common situations: Optional-style lookups unwrapped unsafely; passing an un fetched/unset relation field straight to getIdentifier; helper methods that accept Object without null checks.
Related errors
- {} is not an entity
- Class '<componentClassName>' is an '@Embeddable' type and ma
- Property '<propertyName>' may not be annotated '@BatchSize'
- One to many association '<propertyName>' was annotated '@Col
- Collection '<propertyName>' was annotated '@Collate'
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/dc08fb4928500cfb.
Report an issue: GitHub.