Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA, inheritance and instanceof

I use JPA inheritance with JOIN strategy (JPA2/Hibernate). I have an abstract general Event entity with shared fields (date, time, place etc), and its children, let's say OutdoorEvent, ClassicalMusicEvent etc. with specific fields for each type. I make a search over all Event's, getting a List<Event> which I display. The processing for each event type is different though, so I need to figure out the type of event for each Event object. Now here's the problem. I came up with two solutions. First, the instanceof keyword:

if (event instanceof OutdoorEvent) {
   ...
} else if (event instanceof OtherKindOfEvent) {
   ...
} etc.

Second, I add a transient enum field to the Event entity and set this field in every children type's 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 to this?

like image 856
John Manak Avatar asked Jun 10 '11 08:06

John Manak


People also ask

What is a JPA inheritance?

JPA Inheritence Overview Inheritence is a key feature of object-oriented programming language in which a child class can acquire the properties of its parent class. This feature enhances reusability of the code. The relational database doesn't support the mechanism of inheritance.

Which among the following is JPA inheritance strategy?

JPA support three types of inheritance strategies such as SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

Which inheritance strategy is better in hibernate?

If you have a single hierarchy, prefer To use Single Table Inheritance strategy. Joined strategy is better designed when you have a complex hierarchy but it suffers due To performance issues when querying for any entity. Save this answer.

Which JPA inheritance strategies require discriminator column?

Only SINGLE_TABLE inheritance hierarchies require a discriminator column and values. JOINED hierarchies can use a discriminator to make some operations more efficient, but do not require one. TABLE_PER_CLASS hierarchies have no use for a discriminator.


1 Answers

This is a good question because the optimal OOP solution would be to use polymorphism. On your abstract Event class add an abstract 'process' method and then in your subclasses implement the required processing. Then you can just call process() and don't care what the type of subclass it is.

However, you probably want to keep your Event (i.e. data) classes decoupled from the logic so somewhere you're probably going to end up doing instanceof or something like your enum.

I have no preference which, it is possible that one is faster than the other and I would probably go with the enum rather than instanceof for speed (keen to hear if anyone has insight on that).

If you do go with the enum In your example with the events, you should use a switch instead of if..else.

like image 165
brindy Avatar answered Sep 21 '22 12:09

brindy