• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate one-to-many?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Mark
 
Casey Kcins
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I would do for the Doctor class, and I am only posting just stuff regarding Appointments



This assumes you are using Java5 for Generics. And my guess is that you are using hbm.xml files for mapping.

Mark
 
Casey Kcins
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooooohhhh...
Okay. That's what I was confused about.
Lemmie try that out, and see what becomes of it.

I'm not sure if it's going to be bi-directional or unidirectional.
How would I determine that?

I am using hbm files.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casey,
Uni-directional relationships are kind of uncommon (aberrant, whatever), but

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-unidirectional-12m

describes a few of the nuances.


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 ]
 
Ranch Hand
Posts: 686
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm not sure if it's going to be bi-directional or unidirectional.
How would I determine that?



I am not sure why its confusing? you need a unidirectional or bidirectional-- depend on your application requirement.

For watching two dimension relationship you might need to have following strcuture in your hbm.xml file:

You can see in Appointments.hbm.xml

<class name="Appointments">
<set name="Appointments" inverse="true">
<key column="appointmentId">
<one-to-many class="Appointments"/>
</set>


In Doctor.hbm.xml you can see

<class name="Doctor">
<id name="id" column="doctorId">
<generator class="native"/>
</id>
<many-to-one name="appointments"
column="appointmentId"
not-null="true"/>
</class>
 
Casey Kcins
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

<set name="appointments" inverse="true" cascade="save-update">
<key column="doctor_id" not-null="true" />
<one-to-many class="com.servicesoftheweb.scheduler.DoctorsAppointment" />
</set>

Then, the many-to-one relationship in DoctorsAppointment.hmb.xml:

<many-to-one name="doctor" class="com.servicesoftheweb.scheduler.Doctor" column="doctor_id"/>

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.");

surgon.setAppointments(new HashSet());
surgon.getAppointments().add(removeBrain);
removeBrain.setDoctor(surgon);
surgon.getAppointments().add(tripleBypass);
tripleBypass.setDoctor(surgon)

session.saveOrUpdate(surgon);

... Is this the correct way, or am I doing this bassackwards?

Thanks!

-- Casey

[ March 14, 2007: Message edited by: Casey Kcins ]
[ March 14, 2007: Message edited by: Casey Kcins ]
 
Jignesh Patel
Ranch Hand
Posts: 686
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic