title: JPA - Mapping entity to more than one other entity
body: <p>I have two Entities that can be commented by user (lets say for example: news and articles). Is it possible to use one Comment Entity and attach them to the both entities? </p>
  
  <p>I tried it by creating an abstract super class (<code>AbstractCommentableEntity</code>), that handles the relationship to the comments (would be <code>CommentabeEntity1</code> and <code>CommentabeEntity2</code>). The news and article entities then would extend this class and it would be possible to attach comments to them. See my image as well as my code.</p>
  
  <p>But if I implement it like that, I get an Mapping exception. I think thats because I try to map a @MappedSuperclass annotated class. Is there any way how I can get this running?</p>
  
  <p><img src="https://i.stack.imgur.com/YwGYR.png" alt="enter image description here"></p>
  
  <p>My comment entiy would look like this:</p>
  
  <pre><code>@Entity
  public class Comment {
  
      private AbstractCommentableEntity commentableEntity;
  
      @ManyToOne
      @JoinColumn
      public AbstractCommentableEntity getCommentableEntity() {
          return commentableEntity;
      }
  
      public void setCommentableEntity(AbstractCommentableEntity commentableEntity) {
          this.commentableEntity = commentableEntity;
      }
  }
  </code></pre>
  
  <p>The abstract superclass for the commentable entities would look like:</p>
  
  <pre><code>@MappedSuperclass 
  public abstract class AbstractCommentableEntity {
  
      Set&lt;Comment&gt; comments = new HashSet&lt;Comment&gt;();
  
      @OneToMany(mappedBy = "commentableEntity")
      public Set&lt;Comment&gt; getComments() {
          return comments;
      }
  
      public void setComments(Set&lt;Comment&gt; comments) {
          this.comments = comments;
      }
  }
  </code></pre>
  
  <p>And a specific entity would just extend it like that:</p>
  
  <pre><code>public class CommentabeEntity2 extends AbstractCommentableEntity{
  
  }
  </code></pre>