Search code examples
hibernateormhierarchical-datahibernate-mappingobject-relational-model

Entity hierarchy in Hibernate


What's the proper way to create 2 entities where 1 is a parent of the other? For example, we have the following 2 tables:

Email_Outbox
------------
Id
Email_Id
Date_Sent

Email_Outbox_Schedule
---------------------
Id
Email_Id
Date_Sent
Date_to_Send

Pretty simple, we have emails that go out immediately in EmailOutbox and emails that go out at a specified date in EmailOutboxSchedule. So ideally, this is a class hierarchy in Java where EmailOutboxSchedule extends EmailOutbox. Then we only need to specify 1 extra field, dateToSend, and can take advantage of typical object hierarchy features. Is there a problem doing this with hibernate? Do I need to annotate the 2 entities in any special way?


Solution

  • Going by this document, the solution I see is to use the @MappedSuperclass for a parent class Email_Outbox and then have two child classes Scheduled_Email_Outbox and Immediate_Email_Outbox.

    Immediate_Email_Outbox would be empty except for the @Entity annotation.

    An empty class is hardly elegant, I admit, but it seems like it would work. (And hibernate and elegance are somewhat incompatible anyway.)