-->
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: Generating natural ids
PostPosted: Mon Aug 04, 2008 9:03 pm 
Newbie

Joined: Mon Aug 04, 2008 8:29 pm
Posts: 2
Location: San Francisco
I have a use case I'm having trouble finding documentation for. I would like to generate a natural id when an object is saved to the database for the first time. The object has three ids, a sequential integer surrogate id generated by the database that is used in joins (the primary key), a uuid intended for collating data between separate installations of the system and a short, randomly generated hexidecimal string that is exposed to users.

It would be convenient to be able to do something like the following in the case of the uuid:

Code:
<natural-id>
    <property name="uuid" column="uuid">
        <generator class="guid"/>
    </property>
</natural-id>


I understand the equivalent can be achieved through a trigger or by setting the relevant setter on the object before it is persisted. I've also come accross a reference in one of the FAQs to writing a CompositeUserType and then defining a custom IdentifierGenerator (I assume the custom IdentifierGenerator isn't relevant here).

There is an open issue for a related feature concerning allowing a generator in a composite id element.

http://opensource.atlassian.com/project ... e/HHH-2060

What is a good practice in the above use case? Am I missing something in trying to do things this way?

Thanks in advance for any help.

Eric

Hibernate version: Hibernate 3.2


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 1:06 am 
Newbie

Joined: Mon May 19, 2008 12:42 am
Posts: 10
Hi Eric,
Just curious how you were able to solve this problem. Can you please post the details here? . I'm running into the very same issue.

Can we have the PK class independent of the class to be persisted.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 4:24 pm 
Newbie

Joined: Mon Aug 04, 2008 8:29 pm
Posts: 2
Location: San Francisco
In my case I had some pretty complex custom SQL that I needed to use, and I was getting CGLIB exceptions. I didn't have time to wade through the documentation and the Hibernate code to figure out whether the error was mine or a bug in Hibernate (I assume it was my own error). I needed to move forward, so I removed Hibernate.

Before I got to that point, I found a class called RandomGUID that generates a pseudo-random (rather than purely timestamp-based) guid. When I created a new object I called this class to generate the guid before persisting the object to the store for the first time.

http://javaexchange.com/aboutRandomGUID.html

Hope this helps.


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.