title: Spring data JPA NPE in validateQuery with custom entityManagerFactory and Query annotation
body: <p>I'm working on a Spring Boot project using spring-boot-starter-parent 1.2.0.RELEASE. I'm attempting configure spring to use Hibernate's JTASssionContext through the spring SpringJtaSessionContext extension.</p>
  
  <p>In my <code>@Configuration</code> class I'm returning the JTA configured <code>LocalContainerEntityManagerFactoryBean</code>.</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(repositoryFactoryBeanClass = JpaRepositoryFactoryBean.class)
  @EntityScan(basePackageClasses = Event.class)
  public class JPAConfig {
  
      @Autowired
      private DataSource dataSource;
  
      @Autowired
      private JpaVendorAdapter jpaVendorAdapter;
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
          LocalContainerEntityManagerFactoryBean   localContainerEntityManagerFactoryBean = new    LocalContainerEntityManagerFactoryBean();
          localContainerEntityManagerFactoryBean.setJtaDataSource( dataSource  );
          localContainerEntityManagerFactoryBean.setJpaVendorAdapter( jpaVendorAdapter );
          HashMap&lt;String, Object&gt; properties = new HashMap&lt;&gt;();
          properties.put("hibernate.current_session_context_class", "jta");
          properties.put("javax.persistence.transactionType", "JTA");
          properties.put( "hibernate.dialect", "org.hibernate.dialect.SQLServerDialect" );
          properties.put( "hibernate.format_sql", "true" );
          properties.put( "hibernate.ejb.naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy");
          localContainerEntityManagerFactoryBean.setJpaPropertyMap( properties );
          return localContainerEntityManagerFactoryBean;
      }
  
  }
  </code></pre>
  
  <p>Everything seems to be wired up but I am running into a NPE inside of <code>org.springframework.data.jpa.repository.query.SimpleJpaQuery</code></p>
  
  <pre><code>/**
   * Validates the given query for syntactical correctness.
   * 
   * @param query
   * @param em
   */
  private final void validateQuery(String query, String errorMessage) {
  
      if (getQueryMethod().isProcedureQuery()) {
          return;
      }
  
      EntityManager validatingEm = null;
  
      try {
          validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
   ....
  </code></pre>
  
  <p>The call to <code>getEntityManager().getEntityManagerFactory().createEntityManager();</code> results in a NPE that seems to be coming out of <code>createEntityManager()</code></p>
  
  <p>The supplied class for <code>getEntityManager()</code> seems to be the <code>LocalContainerEntityManagerFactoryBean</code> that I defined in JPAConfig. The supplied <code>EntityManagerFactory</code> seems to be <code>org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean</code>. It seems like something inside of createEntityManager() is null.</p>
  
  <p>This results in the following stacktrace</p>
  
  <pre><code>    Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.vnomicscorp.dw.model.Event com.vnomicscorp.dw.webservice.repo.EventRepository.findByCompositeKey(com.vnomicscorp.dw.model.Vehicle,com.vnomicscorp.dw.model.EventType,long)!
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87)
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.&lt;init&gt;(SimpleJpaQuery.java:57)
      at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:70)
      at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:51)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:137)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:202)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:80)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.&lt;init&gt;(RepositoryFactorySupport.java:357)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:192)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
      at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
      ... 240 more
  Caused by: java.lang.NullPointerException
      at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
      at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
      at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
      at org.hibernate.jpa.internal.EntityManagerImpl.&lt;init&gt;(EntityManagerImpl.java:91)
      at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
      at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
      at sun.reflect.GeneratedMethodAccessor48.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:388)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:541)
      at com.sun.proxy.$Proxy76.createEntityManager(Unknown Source)
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:80)
      ... 253 more
  </code></pre>
  
  <p>The caused by line is pointing at this method reflection invoke</p>
  
  <pre><code>    Object retVal = method.invoke(this.nativeEntityManagerFactory, args);
  </code></pre>
  
  <p>I should also add, I am using Spring data rest and have a <code>RepositoryRestMvcConfiguration</code> class as well. </p>
  
  <pre><code>@Configuration
  public class RestRepoConfig extends RepositoryRestMvcConfiguration {
  
      @Override
      protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
          config.exposeIdsFor(Employee.class, Vehicle.class, Customer.class, VehicleMake.class, VehicleModel.class, Event.class);
      }
  }
  </code></pre>
  
  <p>The NPE is being thrown while trying to validate a <code>PagingAndSortingRepository</code> with added methods.</p>
  
  <pre><code>public interface EventRepository extends PagingAndSortingRepository&lt;Event, Long&gt; {
  
      @Query("SELECT e FROM Event AS e WHERE e.vehicle = :vehicle AND e.startTime &lt;= :endTime AND e.endTime &gt;= :startTime")
      public Iterable&lt;Event&gt; findByTimeperiod(@Param("vehicle") Vehicle vehicle,
                                          @Param("startTime") long startTime,
                                          @Param("endTime") long endTime);
  
      @Query("SELECT e FROM Event AS e WHERE e.vehicle = :vehicle AND e.eventType = :eventType AND e.startTime = :startTime")
      public Event findByCompositeKey(@Param("vehicle") Vehicle vehicle,
                                  @Param("eventType") EventType eventType,
                                  @Param("startTime") long startTime);
  }
  </code></pre>