-->
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.  [ 2 posts ] 
Author Message
 Post subject: Confused about unidirectional one-to-one and db design?
PostPosted: Sun May 27, 2007 1:47 am 
Newbie

Joined: Wed Nov 10, 2004 2:46 am
Posts: 5
Hibernate version:3.2

i have two entity - Staff and Storeroom, there is a unidirectional association between them, Staff need know Storeroom,Stroeroom don't need know Staff at all. so, i first design pojo and table like these:

1) pojo
Code:
public class Staff implements Serializable {
     private String id;
     private String name;
     private Storeroom storeroom;
   
     public Staff(){};
     //getter and setter of all above properties......
}

public class Storeroom implements Serializable {
     private String id;
     private String location;
   
     public Storeroom(){};
     //getter and setter of all above properties......
}


2)table
Code:
table Staff (staffid,name);//staffid is primary key
table Storeroom(roomid,staffid,location);//roomid is primary key and  staffid is foreign key


if i design like above, i was confused by how to design hbm file? from hibernate doc, i know there are two way to present one-to-one association:

one) share primary key.
if use this way, my Staff.hbm file should like
Code:
Staff.hbm.xml
<hibernate-mapping>
.....
<one-to-one name="storeroom"
class="Storeroom"
cascade="save-update"/>
.......
</hibernate-mapping>

and i must fix Storeroom to add a Staff reference
Code:
public class Storeroom implements Serializable {
     private String id;
     private String location;
     private Staff staff;
   
     public Storeroom(){};
     //getter and setter of all above properties......
}
Storeroom.hbm.xml
<hibernate-mapping>
   <id name="id" column="roomid">
         <generator class="foreign">
                <param name="property">staff</param>
         </generator>
   </id>
...
<one-to-one name="staff" class="Staff" constrained="true"/>
.......
</hibernate-mapping>

that is to say, it become a bidirectional association.?? it is not what i want, i only need reference stroeroom in staff..
and i need fix table to
Code:
table staff(staffid,name) staffid is pk
table storeroom(roomid,location) roomid is pk and fk



two)use foreign key associations
my staff.hbm should like
Code:
Staff.hbm.xml
<hibernate-mapping>
.....
<many-to-one name="stroeroom"
                     class="Storeroom"
                     column="roomid"
                     cascade="save-update"
                     unique="true"/>
....
</hibernate-mapping>

to use this way i need fix my table to:
Code:
table staff(staffid, roomid, name)staffid is pk,roomid is fk
table storeroom(roomid,location)


but this looks like confusing, i need a one-to-one association but i must write a many-to-one with a unique in my hbm file. and i also don't want to add a new field to my staff table. if there are many one-to-one association between Staff and other entity, does i must add so many new field to staff table?

can i just add a staffid in storeroom table, and write hbm and pojo reference storeroom in staff, no need let storeroom know staff?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 27, 2007 1:07 pm 
Newbie

Joined: Tue May 08, 2007 12:25 am
Posts: 18
it seems as though your storeroom is its own entity and should have its own primary id, since its life cycle is not dependent on the staff.

Staff is its own entity. and you would need a foreign Id in the Staff table back to the storeroom. I hope this helpsl


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