Hibernate - Persistent polymorphic compounds

I'm trying to figure out how best to implement polymorphic one-to-many sleep mode.

For example:

@MappedSuperclass public class BaseEntity { Integer id; // etc... } @Entity public class Author extends BaseEntity {} @Entity public class Post extends BaseEntity {} @Entity public class Comment extends BaseEntity {} 

And now I would also like to save audit information with the following class:

 @Entity public class AuditEvent { @ManyToOne // ? BaseEntity entity; } 

What is the appropriate mapping for auditEvent.entity ? Also, how does Hibernate actually persist in this? Will a series of connection tables be generated ( AuditEvent_Author , AuditEvent_Post , AuditEvent_Comment ) or is there a better way?

Note that I would prefer that my other entity classes do not display the other side of the connection (for example, List<AuditEvent> events on BaseEntity ), but if this is the cleanest implementation method, then that will be enough.

+7
java hibernate
source share
1 answer

The mapped superclass is not an entity and therefore cannot be part of an association (as recalled in EJB-199 ). So either:

  • create a BaseEntity abstract and use the TABLE_PER_CLASS strategy (see this previous answer )
  • AuditableEntity another AuditableEntity object to the hierarchy and use the most appropriate inheritance strategy for your use. li>
  • consider using envers
+4
source share

All Articles