Subtle behavior difference between OpenJPA and Hibernate
Recently I’ve stumbled upon a weird snippet of code in a project I’ve been involved into.
The previous developer set some entity relations to null before deleting it. I was wondering why he did so.
The project uses Hibernate.
So I decided to investigate this.
Here are the components (these classes are made up only for this case) :
And here is a test case :
With the flat xml dataset :
Note that I’m not executing this test in a transaction, the transaction demarcation is on the findByArtistName() method.
I’ve launched this test case with both OpenJPA (2.2.1) and Hibernate (3.6.9)
With OpenJPA, I get the following :
This seems normal : since I delete the artist and there is a CascadeType.ALL associated to albums, it alse deletes the latter, even though I set it null.
Now let’s try with Hibernate
Here’s a completely different story since the test pass !
So it means that setting the albums collection to null really has an impact on the behavior.
Better, if I don’t change the albums field, the test don’t pass anymore, which is what I was expected to see at first.
So it seems Hibernate updates the state of the entity before deleting it in the database.
Is it a correct behavior ? Can we change this with a property ?
I must say that I’m very intrigued by this behavior. I must investigate further in order to have the final word on this, but time lacks.
If someone has the answer to this, please leave a comment.