title: Generic Spring Data JPA repository implementation to load data by class type
body: <p>I am using Spring Data JPA 1.4.3.RELEASE with Hibernate 4.2.7.Final
  I was able to successfully create a Base Repository class, something on the lines of :
  <a href="http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/repositories.html#repositories.custom-behaviour-for-all-repositories" rel="nofollow noreferrer">http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/repositories.html#repositories.custom-behaviour-for-all-repositories</a></p>
  
  <pre><code>            @NoRepositoryBean
              public interface BaseRepository &lt;T extends BaseEntity, ID extends Serializable&gt;
              extends JpaRepository&lt;T, ID&gt;
  
              @NoRepositoryBean
              public class BaseRepositoryImpl&lt;T extends BaseEntity, ID extends Serializable&gt;
              extends SimpleJpaRepository&lt;T, ID&gt; implements BaseRepository&lt;T, ID&gt; {
  </code></pre>
  
  <p>I am able to sucessfully work with :</p>
  
  <pre><code>            public interface FlowerRepository extends BaseRepository&lt;Flower, Long&gt; {
  </code></pre>
  
  <p>Now, I am trying to write an generic implementation(that extends the base repository) to load all reference data like Flower types. 
  That is because I do not want to have a repository for every single "type" data or "reference" data.
  I want to be able to manage that using a generic repository by passing a specific "class" type(that implements a interface specific to reference data type).For E-g</p>
  
  <pre><code>            loadAll(FlowerType.class)
  </code></pre>
  
  <p>I use HBMs to map hibernate entities so I have :</p>
  
  <pre><code>            &lt;?xml version="1.0"?&gt;
              &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
              &lt;hibernate-mapping&gt;
                  &lt;class name="xxx.FlowerType" table="FLWTYP"&gt;
                  &lt;meta attribute="extends" inherit="false"&gt;xxx.BaseReferenceType&lt;/meta&gt;
                  &lt;id name="primaryKey" type="string"&gt;
                      &lt;column name="TYP_CDE" length="5" /&gt;
                      &lt;generator class="assigned" /&gt;
                  &lt;/id&gt;
  
              public class FlowerType extends BaseReferenceType&lt;String&gt; implements ReferenceEntity&lt;String&gt;
  
              public abstract class BaseReferenceEntity&lt;T extends Serializable&gt; extends BaseEntity implements
      ReferenceEntity&lt;T&gt;
  
              public abstract class BaseEntity implements DomainEntity
  
              public interface ReferenceEntity&lt;PK extends Serializable&gt; {
  </code></pre>
  
  <p>Persistence XML :</p>
  
  <pre><code>            &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
              &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
                  &lt;persistence-unit name="xxxPU"
                      transaction-type="RESOURCE_LOCAL"&gt;
                      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
                      &lt;shared-cache-mode&gt;ENABLE_SELECTIVE&lt;/shared-cache-mode&gt;
                      &lt;properties&gt;
                          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /&gt;
                          &lt;property name="hibernate.hbm2ddl.auto" value="none" /&gt;
                          &lt;property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" /&gt;
                          &lt;property name="hibernate.connection.charSet" value="UTF-8" /&gt;
                          &lt;property name="hibernate.jdbc.batch_size" value="100" /&gt;
                          &lt;property name="hibernate.show_sql" value="false" /&gt;
                          &lt;property name="hibernate.format_sql" value="false" /&gt;
                          &lt;property name="hibernate.transaction.flush_before_completion"
                              value="false" /&gt;
                          &lt;property name="hibernate.connection.autocommit" value="false" /&gt;
                          &lt;property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/&gt;
                          &lt;property name="jadira.usertype.autoRegisterUserTypes" value="true" /&gt;
                          &lt;property name="jadira.usertype.databaseZone" value="jvm" /&gt;
                          &lt;property name="jadira.usertype.javaZone" value="jvm" /&gt;
                      &lt;/properties&gt;
                  &lt;/persistence-unit&gt;
              &lt;/persistence&gt;
  </code></pre>
  
  <p>Hibernate config :</p>
  
  <pre><code>            &lt;?xml version="1.0" encoding="UTF-8"?&gt;
              &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
              &lt;hibernate-configuration&gt;
                  &lt;session-factory&gt;
                      &lt;mapping resource="FlowerType.hbm.xml"/&gt;
                      &lt;mapping resource="Flower.hbm.xml"/&gt;
                  &lt;/session-factory&gt;
              &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>So I looked at some tutorial links and SO posts and did some work based on :</p>
  
  <p><a href="https://stackoverflow.com/questions/19765684/spring-jpa-adding-custom-functionality-to-all-repositories-and-at-the-same-time">Spring Jpa adding custom functionality to all repositories and at the same time other custom funcs to a single repository</a></p>
  
  <p>I have :</p>
  
  <pre><code>            @NoRepositoryBean
              public interface CustomReferenceDataRepository&lt;T extends BaseEntity &amp; ReferenceEntity&lt;PK&gt;, PK extends Serializable&gt; {
                  public Map&lt;PK, T&gt; findAll(Class&lt;T&gt; clz);
              }
  
              public interface ReferenceDataRepository extends BaseRepository, CustomReferenceDataRepository {
              }
  
              public class ReferenceDataRepositoryImpl&lt;T extends BaseEntity &amp; ReferenceEntity&lt;PK&gt;, PK extends Serializable&gt;
                      implements CustomReferenceDataRepository&lt;T, PK&gt; {
  
                  @PersistenceContext
                  private EntityManager em;
  
  
                  @Override
                  public Map&lt;PK, T&gt; findAll(Class&lt;T&gt; clz) {
                  //do whatever
                      return null;
                  }
  
              }
  </code></pre>
  
  <p>Ended up with a exception :</p>
  
  <pre><code>            Caused by: java.lang.IllegalArgumentException: Not an managed type: class xxx.BaseEntity
                  at org.hibernate.ejb.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:200) ~[hibernate-entitymanager-4.2.7.Final.jar:4.2.7.Final]
                  at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.&lt;init&gt;(JpaMetamodelEntityInformation.java:68) ~[spring-data-jpa-1.4.3.RELEASE.jar:na]
                  at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65) ~[spring-data-jpa-1.4.3.RELEASE.jar:na]
                  at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:146) ~[spring-data-jpa-1.4.3.RELEASE.jar:na]
  </code></pre>
  
  <p>I understand Hibernate does not manage "BaseEntity" but am not able to figure out what I am missing.</p>
  
  <p>Is my requirement of implementing a generic repository for reference data even possible ?
  If yes, what am I doing wrong ?
  Any guidance appreciated.
  Thanks.</p>