If I remove the unique everyting works fine.
But when I use unique I get a very weird exception.
First let me explain what I am inserting in the database.
My table category has a unique name.
I am trying to insert:
A
A
A
A
B
B
B
C
Now this is where the strange things happen. I am able to insert A, it reads next one and I get duplicate error which is correct, then it reads next and I get duplicate error which is also correct. However when it is finished with reading all A's and I am saving 'B', I get
Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"
This is very strange. Because 'B' is not yet in the database, so why do I get this error? The code stops there, and nothing more is executed.
Of course A, B and C are just examples
I want to explain a little of how I have implemented this.
I set the objekt category.set(getValue(..)), then I beginTransaction, save and commit.
Then I have a catch exception (where the duplicate exception is handled)
and I retrieve the current field and I perform a session.getTransaction().rollback();
You might ask your self "why does he rollback", and that is a very good question. I tried withouth, and Hibernate threw the same exception after it had handled two A's. It didn't even reach the third and fourth A. So I tried with the rollback and I at least was able to reach all A's before it encountered B and I got the exception.
I have tried to put session.flush() before I save the objekt and also in the exception before I take a rollback. But that resultet with only two A's being read, like when I removed rollback.
I believe this is a bug. I hope I am wrong.
|