-->
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: How configure for a (Optional) OneToOne Composite Primary Ke
PostPosted: Sun Sep 14, 2014 2:41 pm 
Newbie

Joined: Tue Sep 02, 2014 5:45 pm
Posts: 4
I am using Hibernate and have two tables, STUDENTS and DORM_ROOMS, that are related with a composite key:
Code:
STUDENTS table:
CAMPUS(String)   Part of Composite Key
STUDENT_ID (int)   Part of Composite Key
NAME (String)
...

DORM_ROOMS table:
CAMPUS(String)   Part of Composite Key
STUDENT_ID (int)   Part of Composite Key
ROOM_NUMBER(int)
...

The relationship is one to one because a student can be associated with exactly one dorm room and and a dorm room is associated with one student (wow - a private room!). However, not all students have a dorm room.

My initial code (stripped down) looks like:
Code:
FOR STUDENTS:
@Embeddable
public class StudentsPK implements Serializable {
@Column(name = "CAMPUS")
private String Campus;
@Column(name = "STUDENT_ID")
private String StudentID;
...
}


@Entity
@Table(name = "STUDENTS")
public class Students implements Serializable {
@EmbeddedId
private StudentsPK studentsPK;
...
}


FOR DORM_ROOMS:
@Embeddable
public class DormRoomsPK implements Serializable {
@Column(name = "CAMPUS")
private String Campus;
@Column(name = "STUDENT_ID")
private String StudentID;
...
}


@Entity
@Table(name = "DORM_ROOMS")
public class DormRooms implements Serializable {
@EmbeddedId
private DormRoomsPK dormRoomsPK;
...
}

Assume that the database schema is already defined and created. In particular, CAMPUS+STUDENT_ID is a PK for STUDENTS and CAMPUS+STUDENT_ID is a FK for DORM_ROOMS that serves as the PK in that table. At this point I can successfully insert a row into STUDENTS and a row into DORM_ROOMS. I can also retrieve any student from STUDENTS even if the student does not have a dorm room. However, I have not yet "informed" Hibernate about the relationship between the two tables. That is where I am confused.

I tried to "relate" the two tables by using a "JOIN" annotation but I discovered that this causes any attempt to fetch a student that has no dorm room to return an empty result set. I suppose that makes since if "JOIN" states that the tables are to always be viewed as joined then joining a student having no dorm room with no matching rows in the DORM_ROOMS table would result in an empty result set.

Since using a "JOIN" annotation doesn't work, how do I modify my code to describe the relationship between the two tables but still allow me to fetch students that have no matching dorm rooms?

Thank you.


Top
 Profile  
 
 Post subject: Re: How configure for a (Optional) OneToOne Composite Primary Ke
PostPosted: Mon Sep 15, 2014 7:08 pm 
Newbie

Joined: Tue Sep 02, 2014 5:45 pm
Posts: 4
Found the problem! I discovered that in a @OneToOne relationship with a composite key, using a separate FK class to manage the composite key in both entities causes the error. The problem is shown in my original posting where I define and use StudentsPK and DormRoomsPK! Once I changed to use a single "PK" class instead of these two my problem was eliminated. (This doesn't appear to be a well documented requirement!)


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.