This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
Okay. I'm trying to figure out the how this is 'supossed to work in hibernate.
I'm making a real basic scheduler for a doctor's office. Right now, I only have 2 classes: Doctor, and Appointment.
What I'm trying to figure out is, I know a Doctor will have more then one Appointment. But... How do I design that? Will my Doctor class have an Array/Collection of Appointments?
Regardless, I'm fairly sure I don't change the basic getters/setters in the Appointment class... I know I could create an instance of Appointment in Doctor ( Appointment apptOne = new Appointment(vars)), but now what do I do if I want to create another one? Within testing, I can just create another Appointment, but how would I do it from an application standpoint? I'm thinking there would be some sort of NewAppointment() function?
Hopefully this makes sence. Once I know *what* to do, I can code it, I just don't know how to go about it.
Yes, you will have a collection of Appointments in the Doctor object. You will have your getters and setters. Make sure when you do, you set both sides of the relationship. call setDoctor on the appointment class, and call addAppointment on the Doctor class.
I should do a setDoctor() in the appointment class? I would think that the Appointment class doesn't need to know about the Doctor class. In theroy, the Appointment could be any type of appointment...
Also, how would the setAppointment method work in the Doctor class?
*Edit* Here is my Doctor.java class:
..and here is my Appointment.java class:
Thanks for the assistance!
[ March 12, 2007: Message edited by: Casey Kcins ] [ March 13, 2007: Message edited by: Mark Spritzler ]
It also depends on if you are making the relationship bi-directional. If it is truly mapped unidirectional, then you don't have to set both sides. If you do have it bi-directional, then setting both sides is a must for it to correctly be updated in the database.
I feel your pain, since I'm currently struggling through this to try to figure out how to get 2 uni-directional relationships (rather than a bidirectional) to work. I'm not sure what that really means, but...
good luck. [ March 13, 2007: Message edited by: Matt Horton ]
Well, thanks to y'all I figured it out, but I have to admit, it's a little hokey...
Like I mentioned on my above post, putting a reference to the doctor into the appointment makes me unconfortable. Doesn't that require you break abstraction? So, to make myself feel better, I created a class DoctorsAppointment that extends Appointment, and put my reference to Doctor in there.
public class DoctorsAppointment extends Appointment { private Doctor doctor;
public Doctor getDoctor() { return doctor; }
public void setDoctor(Doctor doctor) { this.doctor = doctor; } }
Then, I modified my Doctor.hbm.xml to include a set of DoctorsAppointments:
Now, the part that's really weird, and maybe I'm doing this wrong, is when I tried to persist the set to the database, the foreign-key (Doctor ID), was always coming in null! Why!? After some research, I found out that once I created the appointments for the doctor, I also needed to set the doctor for the appointments! You would *think* that since your adding a child to the parent, you wouldn't think you'd also have to add the parent to the child!
Doctor surgon = new Doctor(); surgon.setName("Dr. Christopher Duncan Turk");
DoctorsAppointment removeBrain = new DoctorsAppointment(); removeBrain.setAppointmentTime(2007, 3, 7, 15, 15); removeBrain.setAppointmentLength(300); removeBrain.setDescription("Remove Brain from Ms. Abby Normal.");
DoctorsAppointment tripleBypass = new DoctorsAppointment(); tripleBypass.setAppointmentTime(2007, 02, 31, 13, 00); tripleBypass.setAppointmentLength(450); tripleBypass.setDescription("Do Triple Bypass on Mr. Rogers.");
You would *think* that since your adding a child to the parent,
Not sure what you mean? since you are adding data in DoctorsAppointment, which is a joint table of Doctor and Appointment, it expects reference data should be exist in both of the tables.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.