Domanda

I am trying to design a specific part of a domain model for a website.

The issue I have is representing a sort of availability matrix with days of the week (Day) and time slots (TimeSlot).

Possible values for Day are:

  • Monday
  • Tuesday
  • Wednesday
  • ...

Possible values for TimeSlot are:

  • Before school
  • Morning
  • Noon
  • Afternoon
  • After school
  • Evening
  • Nighttime

Currently, I have a JPA entity with the two above enums (Day and TimeSlot) as fields:

@Entity
public class DayToTimeSlot {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  private Long id;

  @NotNull(groups = { Default.class, Validation.AdvertisementCreation.class })
  @Enumerated
  private Day day;

  @NotNull(groups = { Default.class, Validation.AdvertisementCreation.class })
  @Enumerated
  private TimeSlot timeSlot;
}

I then populate a DayToTimeSlot reference table with all possible values/combinations.

And finally, I have an Advertisement entity that has a @ManyToMany java.util.Set of DayToTimeSlot instances as follows:

@ManyToMany
private Set<DayToTimeSlot> dayToTimeSlots;

This results in a advertisement_day_to_time_slots join table which can grow very quickly.

Is there not a better way to model this availability matrix?

È stato utile?

Soluzione

I suppose you have only 7Days * 7 TimeSlots = 49 entries in the DayToTimeSlot table and then you only reference them in the Advertisement table.

To remain fully flexible I don't see any better ways to model the availability matrix. But your problem seems to be the association table. I would put myself the following questions: how quickly does it grow? Will you reach 1million in the next 5 years? To search in such 1 million-entries table is not such a big problem.

Independently of your answer to the last question you could think of caching the DayToTimeSlot table.

Besides, if you really do not want to search Advertisment by DayToTimeSlot, you could save the associations as a String field in Advertisment and load them when the getter getDayToTimeSlots() is called. E.g. On save of the Advertisment you itrate through the whole set of DayToTimeSlot, ad build a string of form : "MONDAY_BEFORE_SCHOOL, TUESDAY_NOON,..." and save it. On loading of an Advertisment you process the string in order to create the Set. Of course, in this case you will delete the @ManyToMany reference.

But another feasible solution would be the following (if for every week day you have a single entry of TimeSlot): If you want to search for Advertisment only by day, or if you don't mind writing a bit more complex queries, then you could to add 7 new fields: mondayAvailability, tuesdayAvailability, to the Advertisment entity, save the data there and get rid of the @ManyToMany relationship..

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top