title: EntityManager is always null with @PersistenceContext
body: <p>I'm using Wildfly to create a simple crud app to manage books (find and persist)</p>
  
  <p>But EntityManager is always returning null.</p>
  
  <p>I tried Persistence.createEntityManagerFactory("PU") method, it's working fine.</p>
  
  <p><strong>I need to know why my code is not working..What is missing or wrong ?</strong></p>
  
  <p>Thank you</p>
  
  <blockquote>
    <p><strong>Personel level in Jee = beginner</strong></p>
    
    <p>Wildfly version 14</p>
    
    <p>IDE Intellij</p>
    
    <p>Java 7</p>
    
    <p>JEE 7</p>
    
    <p>ORM EclipseLink</p>
  </blockquote>
  
  <p>Book.java:</p>
  
  <pre><code>package Model;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  
  @Entity
  public class Book {
  
      @Id
      private Long id;
      private String title;
      private String description;
      private Float unitCost;
      private String isbn;
  
      public Book(Long id) {
          this.id = id;
      }
  
      public Book() {
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public Float getUnitCost() {
          return unitCost;
      }
  
      public void setUnitCost(Float unitCost) {
          this.unitCost = unitCost;
      }
  
      public String getIsbn() {
          return isbn;
      }
  
      public void setIsbn(String isbn) {
          this.isbn = isbn;
      }
  }
  </code></pre>
  
  <p>BookService.java</p>
  
  <pre><code>package Service;
  
  import Model.Book;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.PersistenceContextType;
  import javax.transaction.Transactional;
  
  
  @Transactional
  public class BookService {
  
      @PersistenceContext(unitName = "applicationCDBookStorePU", type = PersistenceContextType.EXTENDED)
      private EntityManager em;
  
      public void persist(Book book) {
          em.persist(book);
      }
  
      public Book find(long id) {
          return em.find(Book.class, id);
      }
  }
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"&gt;
  
      &lt;persistence-unit name="applicationCDBookStorePU" transaction-type="JTA"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;properties&gt;
              &lt;property name="eclipselink.jdbc.url" value="jdbc:postgresql://localhost/book"/&gt;
              &lt;property name="eclipselink.jdbc.driver" value="org.postgresql.Driver"/&gt;
              &lt;property name="eclipselink.jdbc.user" value="postgres"/&gt;
              &lt;property name="eclipselink.jdbc.password" value="root"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Test main</p>
  
  <pre><code>package Service;
  
  public class Test {
      public void displayBook(long id) {
          BookService bookService = new BookService();
          System.out.println(bookService.find(id));
      }
  }
  
  </code></pre>
  
  <p>index.jsp</p>
  
  <pre><code>&lt;%@ page import="Service.Test" %&gt;&lt;%--
    Created by IntelliJ IDEA.
    Date: 02/04/2019
    Time: 15:07
    To change this template use File | Settings | File Templates.
  --%&gt;
  &lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;$Title$&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;%
      Test test = new Test();
      test.displayBook(1);
      %&gt;
  &lt; /body&gt;
  &lt;/html&gt;
  
  </code></pre>
  
  <p>StackTrace:</p>
  
  <pre><code>org.apache.jasper.JasperException: java.lang.NullPointerException
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:473)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
      at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
      at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
      at io.undertow.jsp.JspFileHandler.handleRequest(JspFileHandler.java:32)
      at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
      at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
      at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
      at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
      at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
      at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
      at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
      at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
      at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
      at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
      at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
      at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
      at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
      at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
      at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
      at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
      at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
      at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
      at io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
      at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
      at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
      at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
      at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
      at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.lang.NullPointerException
      at Service.BookService.find(BookService.java:22)
      at Service.Test.displayBook(Test.java:6)
      at org.apache.jsp.index_jsp._jspService(index_jsp.java:102)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
      ... 46 more
  
  </code></pre>
  
  <p>Code structure: 
  <a href="https://i.stack.imgur.com/Vo74b.png" rel="nofollow noreferrer">image here</a></p>