Hibernate - Persisting polymorphic joins

Posted by Marty Pitt on Stack Overflow See other posts from Stack Overflow or by Marty Pitt
Published on 2010-05-28T12:27:59Z Indexed on 2010/05/28 12:32 UTC
Read the original article Hit count: 182

Filed under:
|

Hi

I'm trying to understand how to best implement a polymorphic one-to-many in hibernate.

Eg:

@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'd like to also persist audit information, with the following class:

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

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

Note, I'd rather not have my other entity classes expose the other side of the join (eg., List<AuditEvent> events on BaseEntity) - but if that's the cleanest way to implement, then it will suffice.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate