I use JPA inheritance using the JOIN strategy (JPA2 / Hibernate). I have an abstract general Event object with common fields (date, time, place, etc.) And its children, let's say OutdoorEvent, ClassicalMusicEvent, etc. With specific fields for each type. I do a search on all events, getting the List<Event> that I am showing. The handling for each type of event is different, so I need to find out the type of event for each Event object. Now here is the problem. I came up with two solutions. First, the instanceof keyword:
if (event instanceof OutdoorEvent) { ... } else if (event instanceof OtherKindOfEvent) { ... } etc.
Secondly, I add a transitional enumeration field to the Event entity and set this field in each child type constructor. Then I could do:
if (event.getType() == EventType.OutdoorEvent) { ... } else if (event.getType() == EventType.OtherKindOfEvent) { ... } etc.
Which solution is better or more OOP? Is there any other solution for this?
source share