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)tableCode:
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 associationsmy 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?