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..