title: java.lang.NullPointerException while trying to fetch entity by id
body: <p>I'am using spring and hibernate.</p>
  
  <p>My entity:</p>
  
  <pre><code>package com.igorgorbunov3333.core.entities.domain;
  
  import com.igorgorbunov3333.core.entities.enums.Proceeding;
  
  import javax.persistence.*;
  import java.util.Date;
  
  /**
   * Created by Игорь on 03.04.2016.
   */
  
  @Entity
  @Table(name = "cases")
  @NamedQueries({
      @NamedQuery(name = "getAllCases", query = "SELECT c FROM Case c JOIN FETCH c.client JOIN FETCH c.firstInstance " +
              "JOIN FETCH c.secondInstance JOIN FETCH c.thirdInstance JOIN FETCH c.category")
  })
  public class Case {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long id;
  
      @Column(name = "case_number")
      private String caseNumber;
  
      @ManyToOne
      @JoinColumn(name = "client_id")
      private Client client;
  
      @OneToOne
      @JoinColumn(name = "first_instance_id")
      private FirstInstance firstInstance;
  
      @OneToOne
      @JoinColumn(name = "second_instance_id")
      private SecondInstance secondInstance;
  
      @OneToOne
      @JoinColumn(name = "third_instance_id")
      private ThirdInstance thirdInstance;
  
      @Enumerated(EnumType.ORDINAL)
      private Proceeding proceeding;
  
      @Temporal(TemporalType.DATE)
      @Column(name = "closing_date")
      private Date closingDate;
  
      @ManyToOne
      @JoinColumn(name = "category_id")
      private Category category;
  
      private float price;
  
      public long getId() {
          return id;
      }
  
      public String getCaseNumber() {
          return caseNumber;
      }
  
      public Client getClient() {
          return client;
      }
  
      public FirstInstance getFirstInstance() {
          return firstInstance;
      }
  
      public SecondInstance getSecondInstance() {
          return secondInstance;
      }
  
      public ThirdInstance getThirdInstance() {
          return thirdInstance;
      }
  
      public Proceeding getProceeding() {
          return proceeding;
      }
  
      public Date getClosingDate() {
          return closingDate;
      }
  
      public Category getCategory() {
          return category;
      }
  
      public float getPrice() {
          return price;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public void setCaseNumber(String caseNumber) {
          this.caseNumber = caseNumber;
      }
  
      public void setClient(Client client) {
          this.client = client;
      }
  
      public void setFirstInstance(FirstInstance firstInstance) {
          this.firstInstance = firstInstance;
      }
  
      public void setSecondInstance(SecondInstance secondInstance) {
          this.secondInstance = secondInstance;
      }
  
      public void setThirdInstance(ThirdInstance thirdInstance) {
          this.thirdInstance = thirdInstance;
      }
  
      public void setProceeding(Proceeding proceeding) {
          this.proceeding = proceeding;
      }
  
      public void setClosingDate(Date closingDate) {
          this.closingDate = closingDate;
      }
  
      public void setCategory(Category category) {
          this.category = category;
      }
  
      public void setPrice(float price) {
          this.price = price;
      }
  }
  </code></pre>
  
  <p>spring-config.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" &gt;
  
      &lt;!--@Transaction annotation support --&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;!--Scanning components in base-package (look for annotations) --&gt;
      &lt;context:component-scan base-package="com.igorgorbunov3333"/&gt;
  
      &lt;!--Activates various annotations to be detected in bean classes: Spring's @Required and @Autowired and so on--&gt;
      &lt;context:annotation-config/&gt;
  
      &lt;!--Обеспечивает работу с транзакциями в Spring --&gt;
      &lt;bean id="transactionManager"
            class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="emf"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Datasource. Источник данных - база MySQL --&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/database" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="root" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- EntityManagerFactory --&gt;
      &lt;bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;!--Поставщик данных - hibernate--&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
          &lt;/property&gt;
          &lt;!--поиск сущностей в этом пакете--&gt;
          &lt;property name="packagesToScan"
                    value="com.igorgorbunov3333"/&gt;
          &lt;!--детали конфигурации поставщика постоянства (hibernate) --&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;
                      org.hibernate.dialect.MySQL5Dialect
                  &lt;/prop&gt;
                  &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.fetch_size"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;10&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here I'm getting NullPointerException:</p>
  
  <pre><code>public Case findById(long id) {
      return entityManager.find(Case.class, id);
  }
  </code></pre>
  
  <p>I sure to you - id is correct, but entity can't be found. But when I'm getting all entities all fine:</p>
  
  <pre><code>public List&lt;Case&gt; findAll() {
      Query query = entityManager.createNamedQuery("getAllCases");
      return query.getResultList();
  }
  </code></pre>
  
  <p>What is the reason?</p>
  
  <p>Full track of NPE:</p>
  
  <pre><code>09-Oct-2016 12:40:41.650 SEVERE [http-nio-8080-exec-7] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [ShowSingleCase] in context with path [] threw exception
   java.lang.NullPointerException
      at com.igorgorbunov3333.core.entities.service.impl.CaseServiceImpl.findById(CaseServiceImpl.java:32)
      at com.igorgorbunov3333.web.servlet.display.single.ShowSingleCase.doGet(ShowSingleCase.java:29)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
      at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1096)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:760)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1480)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>My CaseServiceImpl:</p>
  
  <pre><code>package com.igorgorbunov3333.core.entities.service.impl;
  
  import com.igorgorbunov3333.core.entities.domain.Case;
  import com.igorgorbunov3333.core.entities.enums.CaseStatus;
  import com.igorgorbunov3333.core.entities.service.api.CaseService;
  import com.igorgorbunov3333.core.util.DateUtil;
  import org.springframework.stereotype.Repository;
  import org.springframework.stereotype.Service;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  import java.util.*;
  
  /**
   * Created by Игорь on 27.07.2016.
   */
  
  @Repository
  @Service("CaseService")
  public class CaseServiceImpl implements CaseService {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      public Case findById(long id) {
          return entityManager.find(Case.class, id);
      }
  }
  </code></pre>
  
  <p>Servlet:</p>
  
  <pre><code>package com.igorgorbunov3333.web.servlet.display.single;
  
  import com.igorgorbunov3333.core.entities.domain.Case;
  import com.igorgorbunov3333.core.entities.service.api.CaseService;
  import com.igorgorbunov3333.core.entities.service.impl.CaseServiceImpl;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  import static com.igorgorbunov3333.web.util.Views.SINGLE_CASE_PAGE;
  
  /**
   * Created by Игорь on 09.08.2016.
   */
  @WebServlet(name = "ShowSingleCase", urlPatterns = "/showSingleCase")
  public class ShowSingleCase extends HttpServlet {
      private CaseService caseService = new CaseServiceImpl();
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request, response);
      }
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          long id = Long.valueOf(request.getParameter("caseID"));
          Case c = caseService.findById(id);
  
          request.setAttribute("case", c);
  
          request.getRequestDispatcher(SINGLE_CASE_PAGE).forward(request, response);
      }
  }
  </code></pre>