-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to manage primary key on a single string dbfield?
PostPosted: Sun Aug 28, 2005 10:25 am 
Newbie

Joined: Sun Aug 28, 2005 9:31 am
Posts: 4
Hi all.
I am new to this forum and I have been dealing with Hibernate, through Hibernate Synchronizer Eclipse plugin, since 2 weeks.

I am developing a client server java app (no container) and I am dealing with an anagrafic-like table.

My question is: what could be the proper hibernate-mapping for a primary key on a single string dbfield (string = *char)? The string values are nicknames: so they are unique and eligible to be the primary key.

I don't actually need identity field which seems instead to be needed by Hibernate (for mapping java object to table rows).

How can I avoid this or, secondarily, how can I let identity field work in conjunction with my actual primary key string field?

Consider, for example, that "john70" is a primary key, actually a row identifier in the table.
A composite primary key, which would be the couple (id, nickname), would lead to the possibility of having two distint rows with the same nickname (!!):

1, "john70", ...
2, "john70", ...

Can anyone help me please?
Thanks in advance and regards.

Hibernate version: 3.0.5 (production)

SQL Server 2000 8.0


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 28, 2005 1:11 pm 
Newbie

Joined: Wed Aug 03, 2005 11:56 am
Posts: 7
Hi Andrew,

You can use "composite-id" tag for composite key as follows:

Code:
<composite-id name="key" class="Your Custom Key Class" >
            <key-property name="id" column="ID_COLUMN" type="java.lang.Integer"/>
            <key-property name="nickname" column="NICKNAME_COLUMN" type="java.lang.String"/>
</composite-id>


Thanks,
Sanjay


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 28, 2005 1:27 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
for the first part...natural id as primary key, you can use 'assigned' generator.

http://www.hibernate.org/hib_docs/v3/re ... d-assigned


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 4:25 am 
Newbie

Joined: Sun Aug 28, 2005 9:31 am
Posts: 4
Hi guys, I wanna really thank you for your replies.

I will put in practice your advices as soon as I can, at worst on next weekend. (that's a personal project of my own, not my actual work :-) ).

Being sure that you deserve lots of credits ;-), I will let you know if your suggestions work good for my project.

See you soon,
Andrew


Top
 Profile  
 
 Post subject: Re: How to manage primary key on a single string dbfield?
PostPosted: Tue Aug 30, 2005 6:26 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
andrew73 wrote:
Consider, for example, that "john70" is a primary key, actually a row identifier in the table.
A composite primary key, which would be the couple (id, nickname), would lead to the possibility of having two distint rows with the same nickname (!!):

1, "john70", ...
2, "john70", ...



It must be better to have generated id as primary key, probably user will want to change nickname (mutable key is not a good candidate for primary key) just add "UNIQUE" constraint for nickname (it is a key too) , composite primary key doe's not make sence in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 10:25 am 
Newbie

Joined: Sun Aug 28, 2005 9:31 am
Posts: 4
baliukas wrote:

Quote:
It must be better to have generated id as primary key, probably user will want to change nickname (mutable key is not a good candidate for primary key) just add "UNIQUE" constraint for nickname


I think you are right.

Anyway I would like to clarify the aim of my java app for you to better advice me.

It's a web spider that collects user profiles from a web community (in which users must be registered).
Users aren't known in advance in my database. They instead have to be provisioned to my database by the spider. Moreover their nicknames are unique and immutable as requirement.

So the most typical task the spider has to execute is a "saveorupdate" of the profiles that it collects by surfing automatically and parsing the html source of the profile pages.

My question is: how can I implement proper update policy based on nickname equality? (Note that "save" isn't yet problematic by following your previous advice)

I have read in the "Equals and HashCode" document in the community area at http://www.hibernate.org/109.html that is goog thing to keep separate oid from actual business row identifier...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 4:34 am 
Newbie

Joined: Sun Aug 28, 2005 9:31 am
Posts: 4
anar wrote:

Quote:
for the first part...natural id as primary key, you can use 'assigned' generator.


Ok, it works!

Maybe it's not a very performing solution, but by forcing dynamic inserts and updates and no optimistic-lock strategy (the spider already manages concurrency of web requests and unicity of profiles being processed!) the performance is quite good :-)

Thank you very much, you definitevely deserve my credit!

Andrew


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