title: Hibernate - ManyToOne & Inheritance / JOINED / mappedBy
body: <p>I have some problems with inheritance mapping. Here my database structure:</p>
  
  <p><img src="https://i.stack.imgur.com/9PPZS.png" alt="enter image description here"></p>
  
  <p>And associated entities:</p>
  
  <h3>AbstractEntity:</h3>
  
  <pre><code>@MappedSuperclass
  public abstract class AbstractEntity&lt;ID extends Serializable&gt; implements Serializable {
  
  @Id @GeneratedValue(strategy = IDENTITY)
  @Column(unique = true, updatable = false, nullable = false)
  private ID id;
  
  public ID getId() {
  return id;
  }
  @SuppressWarnings("unused")
  public void setId(ID id) {
  this.id = id;
  }
  </code></pre>
  
  <h3>UserAcitvity entity:</h3>
  
  <pre><code>@Entity @Table(name = "user_activity")
  @Inheritance(strategy = JOINED)
  @AttributeOverride(name = "id", column = @Column(name = "ua_id"))
  public abstract class UserActivity extends AbstractEntity&lt;Long&gt; {
  
  @ManyToOne(cascade = { MERGE, PERSIST }, fetch = LAZY)
  @JoinColumn(name = "ua_user_id")
  private User user;
  
  ...
  }
  </code></pre>
  
  <h3>Comment entity:</h3>
  
  <pre><code>@Entity @Table(name = "comment")
  @PrimaryKeyJoinColumn(name = "cm_id")
  public class Comment extends UserActivity {
  
  @ManyToOne(cascade = { MERGE, PERSIST }, fetch = LAZY)
  @JoinColumn(name = "cm_question_id")
  private Question question;
  
  ...
  }
  </code></pre>
  
  <h3>Question entity:</h3>
  
  <pre><code>@Entity @Table(name = "question")
  @PrimaryKeyJoinColumn(name = "qs_id")
  public class Question extends UserActivity {
  
  ...
  
  @OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "question")
  private List&lt;Answer&gt; answers = new ArrayList&lt;&gt;();
  
  @OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "question")
  private List&lt;Comment&gt; comments = new ArrayList&lt;&gt;();
  
  ...
  }
  </code></pre>
  
  <h3>Answer entity:</h3>
  
  <pre><code>@Entity @Table(name = "answer")
  @PrimaryKeyJoinColumn(name = "asw_id")
  public class Answer extends UserActivity {
  
  @ManyToOne(cascade = { MERGE, PERSIST }, fetch = LAZY)
  @JoinColumn(name = "asw_question_id")
  private Question question;
  
  ...
  }
  </code></pre>
  
  <h3>and User entity:</h3>
  
  <pre><code>@Entity @Table(name = "user")
  @AttributeOverride(name = "id", column = @Column(name = "user_id"))
  public class User extends AbstractEntity&lt;Long&gt; {
  
  ...
  @OneToMany(cascade = REMOVE)
  private List&lt;Question&gt; questions = new ArrayList&lt;&gt;();
  
  @OneToMany(cascade = REMOVE)
  private List&lt;Answer&gt; answers = new ArrayList&lt;&gt;();
  
  @OneToMany(cascade = REMOVE)
  private List&lt;Comment&gt; comments = new ArrayList&lt;&gt;();
  ...
  }
  </code></pre>
  
  <h3>Problem:</h3>
  
  <p>When I try to save or delete a <code>User</code> I get an exceptions:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into user_question (user_user_id, questions_qs_id) values (?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>org.hibernate.engine.jdbc.spi.SqlExceptionHelper : 147 = user lacks privilege or object not found: USER_ANSWER
  </code></pre>
  
  <p>Hibernate is trying to create a table: <code>user_question</code> and <code>user_answer</code> which me <strong>do not need</strong>.</p>
  
  <p>What I should doing for fixes ?</p>