title: NullPointerException: Spring configuration error or Kundera JPA bug
body: <p>I'm trying to write a simple Spring app and I'm having trouble getting the Spring Repository to work right. I'm getting a <code>NullPointerException</code> at <code>com.impetus.kundera.query.KunderaQuery.initEntityClass(KunderaQuery.java:390)</code>.</p>
  
  <p><strong>The exception is ultimately caused by <code>collectionRepository.findAll()</code>.</strong></p>
  
  <p>Full stack trace:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.impetus.kundera.query.KunderaQuery.initEntityClass(KunderaQuery.java:390)
      at com.impetus.kundera.query.KunderaQuery.postParsingInit(KunderaQuery.java:353)
      at com.impetus.kundera.query.QueryResolver.getQueryImplementation(QueryResolver.java:80)
      at com.impetus.kundera.persistence.PersistenceDelegator.getQueryInstance(PersistenceDelegator.java:557)
      at com.impetus.kundera.persistence.PersistenceDelegator.createQuery(PersistenceDelegator.java:527)
      at com.impetus.kundera.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:379)
      at com.impetus.kundera.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:717)
      at com.impetus.kundera.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:704)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
      at com.sun.proxy.$Proxy33.createQuery(Unknown Source)
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:471)
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:264)
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:60)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:105)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy35.findAll(Unknown Source)
      at com.example.spring.controller.CollectionController.listCollections(CollectionController.java:29)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>I've debugged it nearly as far as I can go and there's a few causes for concern. Going back to the <code>EntityManagerImpl</code> <a href="https://github.com/impetus-opensource/Kundera/blob/trunk/src/jpa-engine/core/src/main/java/com/impetus/kundera/persistence/EntityManagerImpl.java#L704" rel="nofollow">on line 704</a> the <code>CriteriaQueryTranslator</code> translates the <code>CriteriaQuery</code> to <code>SELECT  FROM CollectionEntity null</code>.</p>
  
  <p>This query obviously has problems. So going deeper into the <code>CriteriaQueryTranslator.translate</code> method (<a href="https://github.com/impetus-opensource/Kundera/blob/trunk/src/jpa-engine/core/src/main/java/com/impetus/kundera/persistence/CriteriaQueryTranslator.java#L122" rel="nofollow">line 122</a>):</p>
  
  <pre><code>builder.appendAlias(from.getAlias() != null ? from.getAlias() : select.getAlias());
  </code></pre>
  
  <p>The <code>from.getAlias()</code> returns <code>null</code>, therefore the <code>select.getAlias()</code> is appended to the builder (which is also <code>null</code>). So the <code>alias</code> is what is causing the weird JPQL syntax.</p>
  
  <p>So is this a bug in Kundera or do I have some Spring configuration wrong?</p>
  
  <p><strong>The <code>collectionRepository.findAll()</code> in the controller is what generates this error.</strong> </p>
  
  <p>Here's all my configuration and subject java files:</p>
  
  <p>CollectionController:</p>
  
  <pre><code>import com.example.spring.hibernate.CollectionEntity;
  import com.example.spring.repositories.CollectionRepository;
  import com.example.spring.services.CollectionService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.ModelMap;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import javax.annotation.Resource;
  
  @Controller
  public class CollectionController {
  
      @Autowired
      private CollectionRepository collectionRepository;
  
      @RequestMapping(value="/collections", method= RequestMethod.GET)
      public String listCollections(ModelMap model) {
          model.addAttribute("collection", new CollectionEntity());
  
          // THE ERROR HAPPENS HERE (collectionRepository.findAll())
          model.addAttribute("collections", collectionRepository.findAll());
          return "collections";
      }
  }
  </code></pre>
  
  <p>CollectionRepository:</p>
  
  <pre><code>import com.example.spring.hibernate.CollectionEntity;
  import org.springframework.data.jpa.repository.JpaRepository;
  
  public interface CollectionRepository extends JpaRepository&lt;CollectionEntity, Long&gt; {
  }
  </code></pre>
  
  <p>CollectionEntity:</p>
  
  <pre><code>import javax.persistence.*;
  import java.util.Date;
  
  @Entity(name="collection")
  @Table(name = "collection", schema = "", catalog = "mydb")
  public class CollectionEntity {
  
      @Id
      @Column(name = "collection_id")
      private int collectionId;
  
      @Basic
      @Column(name = "collection_name")
      private String collectionName;
  
      @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinColumn(name = "collection_type_id")
      private CollectionTypeEntity collectionType;
  
      @Column(name = "active")
      private boolean active;
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = "created_at")
      private Date createdDate;
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = "updated_at")
      private Date updatedDate;
  
      public Date getCreatedDate() {
          return createdDate;
      }
  
      public void setCreatedDate(Date createdDate) {
          this.createdDate = createdDate;
      }
  
      public Date getUpdatedDate() {
          return updatedDate;
      }
  
      public void setUpdatedDate(Date updatedDate) {
          this.updatedDate = updatedDate;
      }
  
      public int getCollectionId() {
          return collectionId;
      }
  
      public void setCollectionId(int collectionId) {
          this.collectionId = collectionId;
      }
  
      public String getCollectionName() {
          return collectionName;
      }
  
      public void setCollectionName(String collectionName) {
          this.collectionName = collectionName;
      }
  
      public CollectionTypeEntity getCollectionType() {
          return collectionType;
      }
  
      public void setCollectionTypeId(CollectionTypeEntity collectionType) {
          this.collectionType = collectionType;
      }
  
      public boolean isActive() {
          return active;
      }
  
      public void setActive(boolean active) {
          this.active = active;
      }
  }
  </code></pre>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
               version="2.0"&gt;
      &lt;persistence-unit name="maria_pu"&gt;
          &lt;provider&gt;com.impetus.kundera.KunderaPersistence&lt;/provider&gt;
          &lt;class&gt;com.example.spring.hibernate.CollectionEntity&lt;/class&gt;
          &lt;class&gt;com.example.spring.hibernate.CollectionTypeEntity&lt;/class&gt;
  
          &lt;properties&gt;
              &lt;property name="kundera.client.lookup.class" value="com.impetus.client.rdbms.RDBMSClientFactory" /&gt;
              &lt;property name="hibernate.show_sql" value="true" /&gt;
              &lt;property name="hibernate.format_sql" value="true" /&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/&gt;
              &lt;property name="hibernate.connection.driver_class" value="org.mariadb.jdbc.Driver" /&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydb" /&gt;
              &lt;property name="hibernate.connection.username" value="root" /&gt;
              &lt;property name="hibernate.connection.password" value="" /&gt;
              &lt;property name="hibernate.current_session_context_class" value="thread"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;!DOCTYPE web-app PUBLIC
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
  &lt;web-app id="WebApp_ID" version="2.4"
           xmlns="http://java.sun.com/xml/ns/j2ee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  
    &lt;display-name&gt;Spring Hello World Application&lt;/display-name&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
              org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>and finally the dispatcher.servlet.xml:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:jpa="http://www.springframework.org/schema/data/jpa"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
              http://www.springframework.org/schema/data/jpa
              http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:component-scan base-package="com.example.spring" /&gt;
  
      &lt;jpa:repositories base-package="com.example.spring.repositories"/&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="maria_pu"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/views/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>As I said I'm new to Spring so I may just be missing some configuration parameters. Any help is greatly appreciated!</p>