Hibernate common errors
Just two days after I blogged about the errors deleted entity passed to persist, detached entity passed to persist and id to load is required for loading, some people came here from web searches.
As I'm not sure if those explanations were clear enough, I'll try to synthesise here:
- Deleted entity passed to persist. One entity, supposed to be deleted, is still referenced / accessed. In my case, I had a problem with "cascading". The behavior I expected was "on delete cascade" and "on update cascade". I have those constraints well in the database, but they weren't right in the Hibernate annotations. After I changed that, the error stopped. While I was searching why I was getting this error, I've seen people whose problem was different. They had, let's say, a list of entities. Those entities were related to others, and, when deleting, they had to take care of deleting both the link and the persisted entity. This was the most common scenario of this problem, and is better explained here, for example.
- Detached entity passed to persist. One entity, which is supposed to be up to date but is not, is passed to persist. In my case, I was trying to create an entity (an Operation), related to another (the Account where that Operation was taking place). The persist action was failing because my version of the Account was incorrect. The solution was just to update the entity.
- Id to load is required for loading. Trying to access an entity without giving an actual identifier. In my case this was the easiest problem: I was calling find passing a non initialized long. Passing the correct, initialized values fixed the problem.
Testing…
Today I spent all the afternoon testing. It may seem a tedious task, but after a while it gets quite challenging: you know there are errors in your code, so the objective is finding them. And while doing those tests, correcting the actions which showed wrong, I implemented some new ones.
The application is getting closer and closer to an usable state, and I expect it to be running in about two or three weeks.
Today's odd error was a lot easier than the others: id to load is required for loading. Maybe not the most complete description, before you find the place it is failing, but good enough once you go to the correct line number. I was using some fields in an Action which I haven't initialized yet. D'oh!