title: How to implement a Spring Data repository for a @MappedSuperclass
body: <p>I've got a JPA <code>@MappedSuperClass</code> and an <code>@Entity</code> extending it:</p>
  
  <pre class="lang-java prettyprint-override"><code>@MappedSuperclass
  public class BaseClass {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @Column
      private Boolean active;
  
      //getters &amp; setters
  
  }
  
  @Entity
  public class Worker extends BaseClass{
  
      @Column
      private String name;
  
      //getters &amp; setters
  
  }
  </code></pre>
  
  <p>The <code>active</code> field of the base class is a flag for the children entities. Only the active ones should be loaded in the application. Then I've written a generic <a href="http://docs.spring.io/spring-data/jpa/docs/1.6.4.RELEASE/reference/html/repositories.html" rel="noreferrer">Spring Data Proxy interface</a>:</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface Dao&lt;T extends BaseClass, E extends Serializable&gt; extends
          CrudRepository&lt;T, E&gt; {
  
      Iterable&lt;T&gt; findByActive(Boolean active);
  
  }
  </code></pre>
  
  <p>And this one is the interface that should be for <code>Worker</code> data access, properly extending the previous one:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Transactional
  public interface WorkerDao extends Dao&lt;Worker, Long&gt;{}
  </code></pre>
  
  <p>Well, now in my logic layer I've implemented an abstract class which will wrap the common code for <em>CRUD</em> operations over my entities. I'll have a service for each of them, but I want just to inherit from the <code>abstract</code> one. I want to wire the specific repository for each of the services and provide it to the superclass using an <code>abstract</code> method. That's how my superclass is implemented:</p>
  
  <pre class="lang-java prettyprint-override"><code>public abstract class GenericService&lt;E extends BaseClass&gt;{
  
      public abstract Dao&lt;E, Long&gt; getDao();
  
      //Here I've got some common operations for managing 
      //all my application classes, including Worker
  
  }
  </code></pre>
  
  <p>The problem is that the <code>getDao()</code> method uses the <code>E</code> class parameter, which is guaranteed only to be a child of <code>BaseClass</code> and not a <code>javax.persistence.Entity</code>. When I try to access the DAO from my custom service implementation I get this error:</p>
  
  <blockquote>
    <p>Caused by: java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract java.lang.Iterable com.mycompany.model.daos.interfaces.Dao.findByActive(java.lang.Boolean)!
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93)</p>
    
    <p>Caused by: java.lang.IllegalArgumentException: Not an entity: class com.mycompany.model.BaseClass
        at org.hibernate.jpa.internal.metamodel.MetamodelImpl.entity(MetamodelImpl.java:203)</p>
  </blockquote>
  
  <p>Which makes sense, because <code>E</code> is defined as a child of <code>BaseClass</code>. The compiler allows me to write this too: </p>
  
  <pre class="lang-java prettyprint-override"><code>public abstract class GenericService&lt;E extends BaseClass &amp;&amp; Entity&gt;
  </code></pre>
  
  <p>However I get an error in the child Service that says <code>Worker</code> class is not compatible with the signature for <code>E</code>. Does anybody know how to solve this?</p>