-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problems with UNIQUE. Hibernate throws Exception. Bug?
PostPosted: Thu Sep 28, 2006 3:16 pm 
Beginner
Beginner

Joined: Mon Sep 04, 2006 7:18 am
Posts: 45
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 3:34 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Do you allocate just one object that you keep on trying to save to the database as in save, change properties, save again? If you use synthetic IDs and let Hibernate generate the IDs for you then you also need to reset the ID back to 0,null or whatever the not-saved value is set to in the mapping file.

I hope this helps :)

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 3:53 pm 
Beginner
Beginner

Joined: Mon Sep 04, 2006 7:18 am
Posts: 45
No I generate my own ID. This is not the problem.
If the object is already in the database, which is when the exception is thrown, I just get the object from the database in my exception. Like this

category = categoriManager.getCategoriByName(cvs.get("category_name");

This returns the same object as I am trying to save which I am using further down my code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.