javax.persistence.EntityNotFoundException: Unable to find <entity_type> with id <id_value> in Hibernate

Solution Verified - Updated -

Issue

  • An ManyToOne association is defined

    @Entity
    public class Employee {
        @Id
        private String name;
        ...
        @OneToMany(mappedBy = "employee" ...)
        private List<Task> tasks;
    }
    
    @Entity
    public class Task {
        @Id
        private long id;
    
        private String name;
    
        @ManyToOne
        private Employee employee;
    }
    
  • Referential integrity constraints were not created in the underlying database for the Task.employee_name column.
  • A Task row references a non-existent row (with the name 'John Smith') in the Employee table.
  • When loading the Task instance with the unresolvable Employee reference, an exception is raised:

    javax.persistence.EntityNotFoundException: Unable to find support.hibernate.entity.Employee with id John Smith
    

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content