-->
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.  [ 9 posts ] 
Author Message
 Post subject: Two primary keys in one table
PostPosted: Fri Jul 22, 2005 7:31 am 
Newbie

Joined: Fri Jul 22, 2005 7:16 am
Posts: 4
Location: Germany
I have a legacy database, on which I have to develop an application. In one table there are two primary keys (two separate keys, not a composite key consisting of two columns!):
1. an uuid (surrogate key), which I can generate using Hibenate's uuid-algorithm and
2. an application specific id (customer number), which I have to generate using an existing sequence from the database.

The first I can generate using

<id name="id" type="string" unsaved-value="null">
<column name="UUID" sql-type="char(32)" not-null="true" />
<generator class="uuid.hex" />
</id>

the second I can generate using

<id name="custno" type="string" unsaved-value="null">
<column name="custno" sql-type="char(28)" not-null="true" />
<generator class="sequence">
<param name="sequence">CUSTNO</param>
</generator>
</id>

but not both at the same time.

Since I cannot have two <id>-elements in my mapping file I am looking for another possibility to map this table (without modifying it or creating new tables, as this is a legacy DB).

Any ideas?

Hibernate version: 3.0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 8:18 am 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
- You can always keep one as the PK and turn the other into a unique-constrained column. The downside is that the generation of the second key is delegated to your class.

- You can write a custom type and a custom generator for that type. It's more work but generation will be handled by Hibernate.

- Finally, if you have two columns that are unique (meaning they are candidate keys) the tuple is also unique. You can treat them as a composite-key, but they will no longer be generated by Hibernate.

I would chose the first approach, defining a PK based on a sequence and implementing the generation of the UUID key within my class. The mapping would be something like:

Code:
<id name="custno" type="string" unsaved-value="null">
    <column name="custno" sql-type="char(28)" not-null="true" />
    <generator class="sequence">
         <param name="sequence">CUSTNO</param>
    </generator>
</id>
<property name="id" type="string" column="UUID" unique="true" not-null="true"/>


I'm fairly new to Hibernate, so please take my advice with a grain of salt.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 8:26 am 
Newbie

Joined: Fri Jul 22, 2005 7:16 am
Posts: 4
Location: Germany
dprotas wrote:
- You can always keep one as the PK and turn the other into a unique-constrained column. The downside is that the generation of the second key is delegated to your class.

Code:
<id name="custno" type="string" unsaved-value="null">
    <column name="custno" sql-type="char(28)" not-null="true" />
    <generator class="sequence">
         <param name="sequence">CUSTNO</param>
    </generator>
</id>
<property name="id" type="string" column="UUID" unique="true" not-null="true"/>



That is what I am doing right now. I just wondered if I could let Hibernate generate both ids.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 8:48 am 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
ute.clauss wrote:
That is what I am doing right now. I just wondered if I could let Hibernate generate both ids.


You can write a user-type that encapsulates both the customer number and the UUID. You would need to write a custom generator implementing IdentifierGenerator that returns that user-type.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 10:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Your schema makes zero sense. If you hae a UUID, it is by definition unique of its own accord. It is the primary key. You don't need another column.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:22 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
gavin wrote:
Your schema makes zero sense. If you hae a UUID, it is by definition unique of its own accord. It is the primary key. You don't need another column.


AFAIK, there is no database that allows two true primary keys in one table, either. You have to choose one of them (though the other can still be constrained unique)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 4:55 am 
Newbie

Joined: Fri Jul 22, 2005 7:16 am
Posts: 4
Location: Germany
gavin wrote:
Your schema makes zero sense. If you hae a UUID, it is by definition unique of its own accord. It is the primary key. You don't need another column.


Well, as I wrote in the beginning, it is a legacy DB, so I cannot change the schema, whether it make sense or not.

eagle79 wrote:
AFAIK, there is no database that allows two true primary keys in one table, either. You have to choose one of them (though the other can still be constrained unique)


This is true, and I did so. I just wondered whether I could use Hibernate to generate both ids, one as the primary key and one as a unique column. But there seems to be not possibility. By now I have found another solution, although I cannot use Hibernates abstraction from the underlying database anymore, which is the drawback of this solution.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 11:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please do not rate well-intentioned replies as "unhelpful".

Both of the responders in this thread were trying to help you, and hence their posts were literally "helpful".

If you speak English as a second language, please learn what "unhelpful" means:

http://dictionary.reference.com/search?q=unhelpful

ie. it is not the negation of "solves my problem".


(I just edited a much stronger rebuke, to allow for the fact that you may not be a native English speaker.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 4:50 am 
Newbie

Joined: Fri Jul 22, 2005 7:16 am
Posts: 4
Location: Germany
Sorry, but I wasn't aware of the correct meaning (yes, I am not a native speaker). Also, I have just now learned the real importance of the credit points. Is there a way to change ratings?


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