不能在Glassfish v4中使用JPA

时间:2013-08-06 02:57:36

标签: java-ee jpa glassfish glassfish-3 glassfish-4

在Glassfish v4上,在这个超简单的应用程序中,当我访问URL时出现错误:javax.persistence.PersistenceException: No Persistence provider for EntityManager named locallocalhost:8080/demo/main

我认为Glassfish v4应该带有内置提供程序。

以下是war文件中的布局:

$ 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/

更新persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="local">
        <jta-data-source>jdbc/local</jta-data-source>
    </persistence-unit>
</persistence>

Main.java尝试创建并保留对象:

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";
  }
}

Foo.java是一个微不足道的实体:

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;
  }

}

RestApp.java是微不足道的,它成功触发了JAX-RS:

package com.example.demo;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class RestApp extends Application {
}

在Glassfish控制面板中有一个名为local的JDBC连接池,成功 ping到本地MySQL 5.6.13安装。有一个名为jdbc/local的JDBC资源Pool Name: local

如何解决这个问题?谢谢!

已编辑:已添加persistence.xml以上

1 个答案:

答案 0 :(得分:1)

您在错误的位置有两个persistence.xml:

META-INF/persistence.xml
WEB-INF/classes/persistence.xml

相对于war包root的正确位置是

WEB-INF/classes/META-INF/persistence.xml

避免歧义,不要放置超过1个persistence.xml。

此外,如果你使用maven,你必须在src / main / resources上放置persistence.xml,使它在战争中的正确位置汇编