org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [...] when using @NotFound(IGNORE)

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 7
    • 6
  • Hibernate (legacy/non-JPA)
    • 5
    • 4

Issue

  • An entity is defined with an EAGER ManyToOne association (foreign key) which is annotated with @NotFound(IGNORE).

    • A non-null value (e.g. 1234) is stored in the underlying database for the foreign key column.
    • In the table for the One ("primary key") side of the association (e.g. the table for an Employee entity) there is no row that corresponds to the foreign key reference (e.g. no "Employee" primary key has the value 1234).
    • Referential integrity was disabled in the backing datasource to allow storage of inconsistent data.
  • When loading the entity with the non-resolvable foreign key (the Many side), an exception is sometimes encountered (the failure may not always occur).

    ... org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [support.hibernate.entity.Employee#12345]
    	at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:247)
    	at org.hibernate.event.internal.DefaultLoadEventListener.returnNarrowedProxy(DefaultLoadEventListener.java:295)
    	at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:256)
    	at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
    	at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1053)
    	at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:980)
    	at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:714)
    	at org.hibernate.type.EntityType.resolve(EntityType.java:502)
    	at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
    	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
    	at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1140)
    	at org.hibernate.loader.Loader.processResultSet(Loader.java:968)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:916)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:347)
    	at org.hibernate.loader.Loader.doList(Loader.java:2578)
    	at org.hibernate.loader.Loader.doList(Loader.java:2564)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2394)
    	at org.hibernate.loader.Loader.list(Loader.java:2389)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
    	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
    	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:198)
    	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1230)
    	at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    

Resolution

This issue will be addressed in a future release1.

As a workaround, the association's fetch type may be changed from FetchType.EAGER to FetchType.LAZY.

Root Cause

This is a known defect (Content from hibernate.atlassian.net is not included.HHH-14537).

The issue may not be consistently reproducible as it is dependent on previous activity that may load an uninitialized proxy for the missing entity.

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.