title: Cannot use JPA in Glassfish v4
body: <p>On Glassfish v4, in this ultra-simple application, I am getting the error: <code>javax.persistence.PersistenceException: No Persistence provider for EntityManager named local</code> when I access the URL: <code>localhost:8080/demo/main</code></p>
  
  <p>I thought Glassfish v4 is supposed to come with a built-in provider.</p>
  
  <p>The following is the layout in the war file:</p>
  
  <pre><code>$ jar tvf /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/demo.war
      39 Sun Aug 04 17:12:40 HST 2013 META-INF/MANIFEST.MF
       0 Mon Aug 05 16:32:16 HST 2013 META-INF/
     217 Mon Aug 05 16:18:22 HST 2013 META-INF/persistence.xml
       0 Sun Aug 04 17:12:36 HST 2013 WEB-INF/
       0 Mon Aug 05 14:30:06 HST 2013 WEB-INF/classes/
       0 Mon Aug 05 14:45:26 HST 2013 WEB-INF/classes/com/
       0 Mon Aug 05 14:45:26 HST 2013 WEB-INF/classes/com/example/
       0 Mon Aug 05 16:23:28 HST 2013 WEB-INF/classes/com/example/demo/
     236 Mon Aug 05 16:24:26 HST 2013 WEB-INF/classes/com/example/demo/Foo.java
     400 Mon Aug 05 16:25:34 HST 2013 WEB-INF/classes/com/example/demo/Main.java
     168 Mon Aug 05 16:21:04 HST 2013 WEB-INF/classes/com/example/demo/RestApp.java
     564 Mon Aug 05 16:28:10 HST 2013 WEB-INF/classes/com/example/demo/Foo.class
    1006 Mon Aug 05 16:28:10 HST 2013 WEB-INF/classes/com/example/demo/Main.class
     381 Mon Aug 05 16:28:10 HST 2013 WEB-INF/classes/com/example/demo/RestApp.class
     217 Mon Aug 05 16:18:22 HST 2013 WEB-INF/classes/persistence.xml
     284 Sun Aug 04 17:12:40 HST 2013 WEB-INF/glassfish-web.xml
       0 Sun Aug 04 17:12:36 HST 2013 WEB-INF/lib/
  </code></pre>
  
  <p><strong>Update</strong>: <code>persistence.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"&gt;
      &lt;persistence-unit name="local"&gt;
          &lt;jta-data-source&gt;jdbc/local&lt;/jta-data-source&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><code>Main.java</code> tries to create and persist an object:</p>
  
  <pre><code>package com.example.demo;
  
  import javax.persistence.Persistence;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  
  @Path("main")
  public class Main {
    @GET
    @Produces("application/json")
    public String main () {
      Foo foo = new Foo();
      foo.setN(7);
      Persistence.createEntityManagerFactory("local").createEntityManager().persist(foo);
      return "main";
    }
  }
  </code></pre>
  
  <p><code>Foo.java</code> is a trivial entity:</p>
  
  <pre><code>package com.example.demo;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  
  @Entity
  public class Foo {
  
    private int n;
  
    @Id
    public int getN () {
      return n;
    }
  
    public void setN (int n) {
      this.n = n;
    }
  
  }
  </code></pre>
  
  <p><code>RestApp.java</code> is trivial, and it successfully triggers JAX-RS:</p>
  
  <pre><code>package com.example.demo;
  
  import javax.ws.rs.ApplicationPath;
  import javax.ws.rs.core.Application;
  
  @ApplicationPath("/")
  public class RestApp extends Application {
  }
  </code></pre>
  
  <p>In Glassfish control panel there is a JDBC Connection Pool called <code>local</code> that <em>successfully</em> pings to a local MySQL 5.6.13 installation. There is a JDBC resource called <code>jdbc/local</code> with <code>Pool Name: local</code>.</p>
  
  <p>How to go about fixing this? Thanks!</p>
  
  <p><strong>Edited:</strong> Added <code>persistence.xml</code> above</p>