-->
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: Newbie: Class Relationships and Hibernate Config
PostPosted: Tue Feb 22, 2005 11:40 pm 
Newbie

Joined: Wed Aug 18, 2004 1:23 am
Posts: 3
Hibernate version: 3b4

I have read the docs (and am a little confused), I have also searched the forum (and google) and haven't found the answer to my question, which is: how I can get the below relationship to be mapped

DB:
user { id not null primary key, username }
userdata {username, key, value }

userdata.username = user.username however user.username can be null i.e. there will be cases where the user.username is null and thus there is no userdata tuples

Java:
user {
private int id
private String username
private HashMap userdata
}

Hibernate:
user {
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<map name="userdata" ????
}

So my question is how do I complete this relationship given that userdata.username can't be a FK as user.username can be null?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 2:19 am 
Newbie

Joined: Mon Sep 06, 2004 1:39 pm
Posts: 18
Location: Utah, USA
My response depends somewhat on whether this is a legacy system or not. If it is not a legacy database then I would recommend using integer primary keys in each table. This will not limit you in any way from searching the relationship by username.

Also it would appear from your database description that the relationship between user and userdata is one-to-one. That is there is only ever one userdata record per user record. In this case it does not make sense to me to use a collection (HashMap) to store the value. I recommend using a <component> mapping.

If you intend for there to be multiple userdata records for each user then userdata.username is a foreign key and you should choose a unique value for the primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 3:52 am 
Newbie

Joined: Wed Aug 18, 2004 1:23 am
Posts: 3
Thanks for your reply dwatrous...

It is not a legacy system. It is for competitions that run on a portal site. Some competitions will be closed (require users to be logged on) whilst others will be open (no logon req'd).

The userdata table will have more than 1 tuple for each username (hence not a primary key). The user table already has an integer id which links users to their entries. The username field must allow a non-unique value to accomodate the open competitions.

As user.username is not unique it cannot be a FK however it's relationship from the perspective of the userdata table is that of a FK.


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.