| Author |
Hibernate problem - not sure why not working....
|
alex jamison
Greenhorn
Joined: Jun 10, 2009
Posts: 7
|
|
I am getting the following error:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations):
and am not sure how it is happening...
What I am doing is this:
basically I am doing 3 things - adding some objects to the dataObject, deleting some services that are related to the dataObject and finally making couple of changes. the code is separate enough that there are 3 pieces instead of one huge piece. Basically it is a page with checkboxes. What I find to be the problem is this:
Scenario 1:
I run add, then delete service then make some updates - i get this cascade error when i am trying to remove all objects.
Scenario 2:
I run add, then update then delete - i get another error that it is expecting row count 1 when i really should have 0.
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
Scenario 3:
if i remove the update part of the code everything works fine - i am not sure why that part of the code
Note that these errors ONLY show up if i were to remove every "service" under dataObject. You can think dataObject as a form with 4 checkboxes and I am basically unchecking/checking them and then "save". If I check 2 and uncheck the other 2 everything's fine. If I uncheck them all then i get the errors. The service update is another check box for something else but under same dataObject so that is causing me problems but i am not sure why.... Anyone can help? (sorry couldnt post too much code...) thanks!
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4931
|
|
I think it might be a matter of the fact that Hibernate is deleting the data from the database, but the POJO is maintaining a reference.
One think I might try is a quick load of the object after the delete. That will refresh your POJO. It might be worth a try?
-Cameron McKenzie
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
alex jamison
Greenhorn
Joined: Jun 10, 2009
Posts: 7
|
|
Cameron Wallace McKenzie wrote:I think it might be a matter of the fact that Hibernate is deleting the data from the database, but the POJO is maintaining a reference.
One think I might try is a quick load of the object after the delete. That will refresh your POJO. It might be worth a try?
-Cameron McKenzie
what do you mean quick load of the object? I tried flushing but that didn't work either. I get Cascade error if i do Add->delete->update... it is probably that the object i update is not up to date with the deletion? If i do Add->update->delete then it is thinking i have more object when i deleted them all... any tricks to "reget" the data to make sure it is up to date?
|
 |
alex jamison
Greenhorn
Joined: Jun 10, 2009
Posts: 7
|
|
Ok, i "solved " the problem but it is a probably not optimal way of doing it... if anyone can improve it would be great:
1. dataObject.addItems(items);
2. session.update(dataObject);
3. service.deleteService(session, servicesToBeDeleted);
3.session.commit();
4. session.close();
5. getNewSession();
6. dataObject = getSameDataObjectAgain();
7. dataObject.update();
8. session.update(dataObject); session.commit();
9. session.close();
it seems when i delete services, the dataObject doesnt get changed so i need to close the session then get a new dataObject(same ID number but updated with deletions) to reflect the changes. This is because when I try to update the dataObject and then commit i am actually committing back the services i deleted so that is why after i close the session and get another dataObject the data is up to date...
Is there a simpler way to do this? It seems such a roundabout way to do it and i fear if any step fails (especially 2nd part) then it would be troublesome to rollback...
|
 |
Vallidevi Appana
Greenhorn
Joined: Jun 30, 2006
Posts: 22
|
|
Hi Alex,
Instead of closing and opening the session back, you can just call refresh method on sesion, so that object state will be refreshed from DB.
so instead of 4 and 5 steps in your code, you can just call,
session.refresh(dataObject);
Hope this works for you.
Regards
Valli
|
 |
 |
|
|
subject: Hibernate problem - not sure why not working....
|
|
|