I have four classes that form the core of a calendaring application. These are a Calendar, a Reservation, an Event, and an Occurrence. A Calendar relates to a set of Events and Occurrences through a set of Reservation objects. In addition, each Event has one or more Occurrences.
Reservation is an abstract class that holds a reference to a Calendar and a char status (approved, rejected, etc.). There are two concrete subclasses of Reservation. An EventReservation contains a reference to an Event and applies to all of its Occurrences (i.e., I want room 104 for a meeting that happens every Tuesday). An OccurrenceReservation applies to just one Occurrence of an Event, and has an Occurrence as a field (i.e., I want room 104 for just this Tuesday, for a meeting that happens more than just that once).
A Calendar only cares about Reservations (for approving, rejecting, etc.) and Occurrences; it does not track Events. It keeps Reservations in an unsorted collection (no need to sort), while Occurrences are in a SortedSet ordered by Occurrence.date. Finally, the Calendar.occurrences only contains Occurrences from Events or Occurrences whose Reservations have status=APPROVED.
Right now, I don't have the Calendar mapping set up to load Occurrences directly; instead, I have Hibernate load the Reservations and I add to the Calendar the Occurrences that correspond to each Reservation, either exactly one (in the case of OccurrenceReservation) or one or more (in the case of EventReservation) like so:
Code:
public void setReservations(Set reservations)
{
this.reservations = reservations;
for (Iterator iterator = reservations.iterator(); iterator.hasNext();)
{
Reservation reservation = (Reservation) iterator.next();
this.occurrences.addAll(reservation.getOccurrences());
}
}
As a result, I cannot effectively use lazy collections for Calendar.reservations and Calendar.occurrences, and thus suffer the performance problems that implies (i.e., a bajillion queries). My question is how can I map a Calendar directly to Occurrences? I basically need the Calendar.occurrences property to be a union of the Calendar=>EventReservation->Event=>Occurrences and Calendar=>OccurrenceReservation->Occurrence ternary relations, sorted by Occurrence.date (where -> denotes one-to-one and => one-to-many).
Visually (in beautiful ASCII art), what I have right now is this:
Code:
Calendar Occurrence
A / A
| / |
one one many
| / |
many one one
| / |
V / V
Reservation <--one-to-one--> Event
What I want to construct (somehow) is this:
Code:
Calendar <-one-to-many-> Occurrence
A / A
| / |
one one many
| / |
many one one
| / |
V / V
Reservation <---one-to-one---> Event
I'd also be open so suggestions on how to modify this model to work more effectively with hibernate. It's important to remember that Occurrences and Events must be individually reservable; otherwise it would just be a simple ternary relation. I know I could create another association table that directly relates a Calendar and an Occurrence, but that redundancy is distasteful, not to mention complicated to implement (seeing as a ternary association requires a Map and the collection of Occurrences needs to be a SortedSet).