-->
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.  [ 10 posts ] 
Author Message
 Post subject: Generator uuid.string produces duplicate primary keys
PostPosted: Sat Sep 25, 2004 9:16 am 
Newbie

Joined: Mon Oct 27, 2003 10:03 am
Posts: 15
I am using Hibernate 2.1.6 in conjunction with an old legacy database transferred to HSQLDB for testing purposes. Since all primary keys are strings I found that I have to use either uuid.hex or uuid.string for generating my primary keys. I decided upon uuid.string.

My DAOs use Spring's HibernateTemplate initialized with a singleton SessionFactory, in my test cases I use Spring's TransactionTemplate for associating a transaction with the current session. The code in question is

Code:
tt.execute(new TransactionCallbackWithoutResult() {

                protected void doInTransactionWithoutResult(
                        TransactionStatus arg0) {

                    for (int i = 2; i < 31; i++) {
                        classUnderTest.save(createValidL500Component(i));
                    }
                }
            });


where tt is Spring's TransactionTemplate. Now if I run this code I get

[code]
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 9:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
um. this should not be possible

do you get the same thing happening with uuid.hex?

perhaps I have a bug in BytesHelper.toBytes(), I'm not good at bits...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 9:44 am 
Newbie

Joined: Mon Oct 27, 2003 10:03 am
Posts: 15
Gavin, thanks for your quick response.

gavin wrote:
um. this should not be possible

do you get the same thing happening with uuid.hex?

perhaps I have a bug in BytesHelper.toBytes(), I'm not good at bits...


No, indeed uuid.hex does the trick. Have been running the whole slew of test cases several times after switching to uuid.hex. No problems thus far. So thanks for a quick solution.

One hint: the problem with uuid.string is not deterministic, i.e. it might happen that one, two or even more test cases pass before Hibernate chokes on a duplicate identifier.

For what it's worth: I am not good at bits either.

Cheers,

Olaf


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 10:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
OK, so what seems to happen is this:

apparently some byte values map to the same character when I create a string using new String(byte[]) - I was not aware of this! I'm guessing this behavior depends upon the character set.

So even though the array of bytes I am generating is unique, the resulting Strings are not.

Does anyone know anyway to fix this? for example, can I specify a character set where all bytes will map to unique characters?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 10:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ok, problem seems to only occur when i pass negative bytes .... so I guess a negative byte value is not a valid character...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 7:50 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
You have to be a bit careful with most charsets, the default one will most likely be ASCII so any characters above 255 could end up being similar. One thing I know for sure for Unicode character set is that there is a cutoff ( somewhere around 55K ) where all byte values above that map to the same character. This is supposed to be where the really exotic languages that have a lot of different characters.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 7:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, I rully expected that some byte values would not have unique *glyphs* - what I was not expecting was that when converting to a String, some of the actual information would be thrown away. ie. new String(bytes).getBytes() is destructive. I had assumed you would just get stuf converted to some chars that may of may not have corresponding glyphs in the current character set.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 8:22 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Sorry didn't explain myself very well.

nvarchar ( SQLServer and Oracle ) exhibit the cutoff behaviour. If you put any char/byte value in above the cutoff it goes into the database and comes out as the cutoff value. All values above this point do this for nvarchar columns. I have a brute force test for unicode on our databases that just builds strings containing everything possible character value and save()/get() each one. What goes in matches what comes out up until the 55K ish point so maybe it is something to do with this?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 8:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ah, that would be bad too. though i think the immediate problem i'm observing happens purely in memory


I think we should probably get rid of uuid.string - does anyone use it? I'm guessing the answer is "no".


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 27, 2004 10:06 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
LOL, funny that you mention that as we were planning on using it to generate some unique values for Workspace names for Oracle Workspace Manager. It goes into a table under Oracle's control and the field is < 32 chars so we couldn't use the Hex generator.

But it is interesting to know that the string one doesn't work :(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.