After reading the answers to this question, and this and this related questions, I'm now confused about how/if to work with an enum having an NxN relationship.
Let's say I have an entity Hotel
, an my hotels can offer some services: sauna, gym, wifi, etc. Following
You should decide whether your Platform is an entity or not. If it's an entity, it can't be an enum
From here, and
The set of valid values is data, not code
From here, and if I understood it all correctly, my approach to modelling this with the enum in DB is to have a hotels
table, a services
table, and a junction table hotels_services
. The services table would have two columns (id, service)
or just one (service)
. When saving hotel information, the validity of the services would be enforced not by the application but by the DB. My Service
class would then have two (Long id, String service
) or just one (String service/value
) fields. My Hotel
class would have a List<Service> services
.
So far, kind of good. But if my application's logic depends somehow on the type of service, I would like to have the enum's type safety. That looks hard, because then I would have to convert them to a ServiceEnum
enum, which sounds just wrong: somewhere I would need to do the convertion, and there I would need to hardcode the possible values, so duplicity. Then, I have this extra class ServiceEnum
that is confusing.
Is this how it's supposed to be? Just give up on using an enum in this case? I can live with it, but I'd like to hear that I need to, or what is it that people do in these cases.