title: Best way of handling entities inheritance in Spring Data JPA
body: <p>I've three <strong>JPA entity</strong> classes <code>A</code>, <code>B</code> and <code>C</code> with the following hierarchy:</p>
  
  <pre><code>    A
      |
  +---+---+
  |       |
  C       B
  </code></pre>
  
  <p>That is:</p>
  
  <pre><code>@Entity
  @Inheritance
  public abstract class A { /* ... */ }
  
  @Entity
  public class B extends A { /* ... */ }
  
  @Entity
  public class C extends A { /* ... */ }
  </code></pre>
  
  <p>Using <strong>Spring Data JPA</strong>, what is the best way to write <strong>repositories</strong> classes for such entities?</p>
  
  <p>I know that I can write these:</p>
  
  <pre><code>public interface ARespository extends CrudRepository&lt;A, Long&gt; { }
  
  public interface BRespository extends CrudRepository&lt;B, Long&gt; { }
  
  public interface CRespository extends CrudRepository&lt;C, Long&gt; { }
  </code></pre>
  
  <p>but if in the class <code>A</code> there is a field <code>name</code> and I add this method in the <code>ARepository</code>:</p>
  
  <pre><code>public A findByName(String name);
  </code></pre>
  
  <p>I've to write such method also in the other two repositories, and this is a bit annoying.. Is there a better way to handle such situation?</p>
  
  <p>Another point I would like to have is that <code>ARespository</code> should be a read-only repository (i.e. extend the <code>Repository</code> class) while the other two repositories should expose all the CRUD operations.</p>
  
  <p>Let me know possible solutions.</p>