title: Spring data JPA: How to use abstract entity
body: <p>I have a few tables named MEMBER, PROVIDER, EMPLOYER etc and they have few common columns like ID, ADDRESS etc. I'm using hibernate to fetch data from these tables using Spring Data JPA. Since I have many similar tables, I created a super entity class and wrote a common method to fetch the data. Here is my super class.</p>
  
  <pre><code>    @Entity
      @Getter
      @Setter
      @NoArgsConstructor
      @ToString
      public abstract class Recipient implements Serializable {
  
        @Id
        @Column(name = "RECIPIENT_ID", unique = true, nullable = false)
        private Long id;
  }
  </code></pre>
  
  <p>Here is a sample entity which extends this super class.</p>
  
  <pre><code>@Entity
  @Getter
  @Setter
  @NoArgsConstructor
  @ToString
  @Table(name = "MEMBER")
  public class Member extends Recipient {
  
    @Column(name = "Member_NAME")
    private String membername;
  
  }
  </code></pre>
  
  <p>Here is the Service</p>
  
  <pre><code>public class RecipientServiceImpl extends AbstractService&lt;Recipient&gt;
      implements RecipientService {
  @Override
    public Recipient findByIdAndType(long id, String type) {
      Recipient recipient = null;
      switch (RecipientType.get(type)) {
        case MEMBER:
          recipient = recipientRepository.findMemberById(id);
        case PROVIDER:
          recipient = recipientRepository.findProviderById(id);
        case EMPLOYER:
          recipient = recipientRepository.findEmployerById(id);
  
      }
  }
  </code></pre>
  
  <p>And here is the repository</p>
  
  <pre><code> public interface RecipientRepository
          extends JpaRepository&lt;Recipient, Long&gt;, JpaSpecificationExecutor&lt;Recipient&gt; {
  
          public Member findMemberById(long id);
          public Provider findProviderById(long id);
          public Employer findEmployerById(long id);
      }
  </code></pre>
  
  <p>Obviously it didn't work since there weren't any tables mapped in the Recipient abstract entity. Is there a way to implement what I was hoping for as a generic fetching method instead of having to create repositories for each entities ?</p>