No row with the given identifier exists как исправить

I am using Hibernate and getting

Exception in thread “main” org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [#271]

What is pretty weird about this error is, that the object with the given id exists in the database. I inserted the problematic record in another run of the application. If I access it in the same run (i.e. same hibernate session) there seem to be no problems retrieving the data.

Just because it could be a fault of the mapping:

public class ProblemClass implements Persistent {
  @ManyToOne(optional = false)
  private MyDbObject myDbObject;
}
public class MyDbObject implements Persistent {
  @OneToMany(mappedBy = "myDbObject")
  private List<ProblemClass> problemClasses;
  @ManyToOne(optional = false)
  private ThirdClass thirdClass;
}

I have absolutely no clue even where to look at. Any hints highly appreciated!

Just to clarify:
The data was inserted in another RUN of the application. It is definitely in the database, as I can see it via an SQL-Query after the application terminated. And after THAT, i.e. when starting the application again, I get the error in the FIRST query of the database — no deletion, no rollback involved.

Addition:
Because it was asked, here is the code to fetch the data:

public List<ProblemClass> getProblemClasses() {
    Query query = session.createQuery("from ProblemClass");
    return query.list();
}

And just to make it complete, here is the generic code to insert it (before fetching in another RUN of the application):

public void save(Persistent persistent) {
    session.saveOrUpdate(persistent);
}

1. Reason

This exception is thrown when we try to load an entity in hibernate with session.load() method and entity is not found. The exception trace looks like this:

Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=?
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [hibernate.test.dto.DepartmentEntity#11]
at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:375)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:140)
at hibernate.test.dto.DepartmentEntity$$EnhancerByCGLIB$$deac2c14.getName()
at hibernate.test.TestHibernateEhcache.main(TestHibernateEhcache.java:21)
Random exceptions
Random exceptions

2. Solution

The correct way to solve this problem is to use session.get() method. Get method will return null if the entity with given identifier is not found.

DepartmentEntity department = (DepartmentEntity) session.get(DepartmentEntity.class, new Integer(11));

if(department == null) {
    //handle null case
}

Another solution is to wrap the session.load() call in try-catch block and handle exception accordingly.

Happy Learning !!

I have the next query :

String queryString = "from Visit vis "
                    + "LEFT OUTER JOIN FETCH vis.pdv vis_pdv ";
return query.list();

After that, I get the next error when I try to access to some pdv:

nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists

The point is I have some corrupt data, so “Visit” has sometimes an id in “pdv” but it doesn’t exist that pdv in the table “PDV”. I would like to handle this in the query, so it doesn’t return corrupt data. Is there any way?

Thanks

Neil Stockton's user avatar

asked Nov 11, 2014 at 9:52

Juanjo's user avatar

There’s a similar issue here: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: Single table query

Basically the answer is: you need to have a consistent database before Hibernate can work with the data.

I understood that you have a Visit.pvd column that is a foreign key into the PVD table but contains data that isn’t reflected in PVD. That’s your integrity violation. What you can do is bypassing Hibernate and collect any Visit.ids that are identifying entities that are invalid:

session.createSQLQuery("SELECT id FROM Visit "
        + "WHERE pvd NOT IN (SELECT p.id FROM pvd)").list();

This gets you a List<Object[]> that you can iterate to get the offending entities. Use that to UPDATE them to not contain invalid references (or just use a plain UPDATE with the WHERE clause I gave).

Community's user avatar

answered Nov 11, 2014 at 11:02

mabi's user avatar

mabimabi

5,2792 gold badges41 silver badges78 bronze badges

In hibernate whene you use join in HQL it returns List so please cast your return satement as return

List<Object[]> query.list(); 

and retrun type as

List<Object[]>

and for your question
try like this

select vis from Visit vis, Pdv pdv where vis.id=pdv.vis.id

this query will return

List<Visit>

answered Nov 11, 2014 at 10:14

mady's user avatar

madymady

1191 gold badge3 silver badges12 bronze badges

2

String queryString = “from Visit vis ”
+ “LEFT OUTER JOIN FETCH vis.pdv “;
return query.list();

you dont need List<Object[]> as your returning List<Visit>

List<Object[]>

is needed when you are returning multiple object from the query.

eg select v.pid,v.pname,v.pvisit from Visit v

answered Feb 21, 2015 at 17:45

linkin's user avatar

linkinlinkin

311 silver badge7 bronze badges

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.

Reason

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(SessionFactoryImpl.java:375)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:140)
at hibernate.test.dto.EmployeeEntity$$EnhancerByCGLIB$$deac2c14.getName()
at hibernate.test.TestHibernateEhcache.main(TestHibernateEhcache.java:17)

Solution

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.

Symptoms

When executing a data Backup or Restore in a Bamboo server, the following message is reported:

Export has failed. Errors encountered while exporting: No row with the given identifier exists: XXXXXXX, of class: com.atlassian.bamboo.resultsummary.AbstractResultsSummary; nested exception is net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: XXXXXXX, of class: com.atlassian.bamboo.resultsummary.AbstractResultsSummary.

String “XXXXXXX” represents an ID of a BUILDRESULTSUMMARY table entry.

Cause

One of the following items can be happening:

1. Bamboo was executing a build when a Backup/Export process was started by the administrator or by the Bamboo backup scheduler system. You can avoid this problem by ensuring that builds are not running when executing a backup.

2. The administrator is restoring a corrupted backup file, which is missing an entry in the BUILDRESULTSUMMARY table.

3. The database is currently inconsistent, missing one of the entries of the BUILDRESULTSUMMARY table.

Resolution

In order to make the database data consistent again, please run the following commands. Their intent is to remove data that is making reference to entries that don’t exist in the BUILDRESULTSUMMARY table.

(warning) Important: Before running the commands, since a backup using the Backup feature may not be able to be run, please create a database dump to ensure that your data will be secure. Next, shut down Bamboo before running these SQL queries against Bamboo database.

DELETE FROM BUILDRESULTSUMMARY_CUSTOMDATA WHERE BUILDRESULTSUMMARY_ID not in (select BUILDRESULTSUMMARY_ID from BUILDRESULTSUMMARY);

DELETE FROM BRS_LINKEDJIRAISSUES WHERE BUILDRESULTSUMMARY_ID not in (select BUILDRESULTSUMMARY_ID from BUILDRESULTSUMMARY);

DELETE FROM COMMIT_FILES WHERE commit_id IN (SELECT commit_id FROM USER_COMMIT WHERE repository_changeset_id IN (SELECT repository_changeset_id FROM REPOSITORY_CHANGESET WHERE buildresultsummary_id NOT IN (SELECT buildresultsummary_id FROM BUILDRESULTSUMMARY)));
	
DELETE FROM USER_COMMIT WHERE repository_changeset_id IN (SELECT repository_changeset_id FROM REPOSITORY_CHANGESET WHERE buildresultsummary_id NOT IN (SELECT buildresultsummary_id FROM BUILDRESULTSUMMARY));		
						
DELETE FROM REQUIREMENT_SET WHERE REQUIREMENT_SET_ID NOT IN (SELECT REQUIREMENT_SET FROM REQUIREMENT) AND REQUIREMENT_SET_ID NOT IN (SELECT REQUIREMENT_SET FROM DEPLOYMENT_ENVIRONMENT) AND REQUIREMENT_SET_ID NOT IN (SELECT REQUIREMENT_SET FROM BUILD);

DELETE FROM TEST_ERROR WHERE RESULT_ID in (SELECT TEST_CASE_RESULT_ID FROM TEST_CASE_RESULT WHERE TEST_CASE_ID in (SELECT TEST_CASE_ID FROM TEST_CASE WHERE TEST_CLASS_ID in (SELECT TEST_CLASS_ID FROM TEST_CLASS_RESULT WHERE BUILDRESULTSUMMARY_ID not in (select BUILDRESULTSUMMARY_ID from BUILDRESULTSUMMARY))));
			
DELETE FROM TEST_CASE_RESULT WHERE TEST_CASE_ID in (SELECT TEST_CASE_ID FROM TEST_CASE WHERE TEST_CLASS_ID in (SELECT TEST_CLASS_ID FROM TEST_CLASS_RESULT WHERE BUILDRESULTSUMMARY_ID not in (select BUILDRESULTSUMMARY_ID from BUILDRESULTSUMMARY)));
			
DELETE FROM TEST_CASE WHERE TEST_CLASS_ID in (SELECT TEST_CLASS_ID FROM TEST_CLASS_RESULT WHERE BUILDRESULTSUMMARY_ID not in (select BUILDRESULTSUMMARY_ID from BUILDRESULTSUMMARY));
			
DELETE FROM TEST_CLASS_RESULT WHERE BUILDRESULTSUMMARY_ID not in (select BUILDRESULTSUMMARY_ID from BUILDRESULTSUMMARY);

DELETE FROM BUILDRESULTSUMMARY_LABEL WHERE LABEL_ID NOT IN (SELECT LABEL_ID FROM LABEL);

Добавить комментарий