0

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

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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>

2 Comments

Thanks. I don't have in Pdv a reference to Visit, just a reference in Visit to Pdv
Sorry am not getting, you mean Visit is child table of Pdv? key of Pdv is in Visit or just reference without foreign key constraint
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.