org.hibernate.ObjectNotFoundException: No row with the given identifier exists Solution


This Exception occurs when Session.load() fails to select a row with the given primary key (identifier value). This exception might not be thrown when load() is called, even if there was no row on the database, because load() returns a proxy if possible. Applications should use Session.get() to test if a row or particular record exists in the database for particular primary key.

In this article, we are discussing the hibernate org.hibernate. ObjectNotFoundException: No row with the given identifier exists.


This exception is thrown when you try to load an entity in hibernate with session.load() method and entity is not found, which means instance of particular entity is not found in the current hibernate seesion context.

Exception trace look like this:

Hibernate: select employee0_.ID as ID0_0_, employee0_.NAME as NAME0_0_ from employee employee0_ where employee0_.ID=? org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [hibernate.test.dto.EmployeeEntity#11] at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound( at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState( at org.hibernate.proxy.AbstractLazyInitializer.initialize( at org.hibernate.proxy.AbstractLazyInitializer.getImplementation( at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke( at hibernate.test.dto.EmployeeEntity$$EnhancerByCGLIB$$deac2c14.getName() at hibernate.test.TestHibernateEhcache.main(


The correct way to solve this problem is to use session.get() method. Get method will return null if there is no record peresent for that entity or if record is not found.

Employee emp = (Employee) session.get(Employee.class, new Integer(11)); //This will throw NullPointerException if entity is not found. System.out.println(emp.getName());

One more solution is that call the session.load() in try catch block and handle exception accordingly.

Solution Worked 0 UpvotesUpvote


Solution Didn't Worked 0 DownvotesDownvote