java - 初始 SessionFactory 创建失败。javax.persistence.PersistenceException

标签 java jsp jpa eclipselink

我正在做网络应用程序。我发现很多问题都有同样的问题

Like this question

但我没有得到正确答案。我的文件夹结构和类路径一切都是正确的,但为什么会发生这个异常我真的不知道。

下面是我的文件夹结构。这里的 persistence.xml 文件位于 src 文件夹的 META-INF 文件夹内

enter image description here

我的 Persistance.xml:

<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_1_0.xsd"
version="1.0">
  <persistence-unit name="jspWithJpa" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.entity.Student</class>
    <properties>    
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/reports" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="root" />
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
    </properties>
  </persistence-unit>
</persistence>

EntityManagerUtil

public class EntityManagerUtil {
  private static final EntityManagerFactory emf;
  static {
    try {
      emf = Persistence.createEntityManagerFactory("jspWithJpa");
    } 
    catch (Throwable ex) {
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
  }

  public static EntityManagerFactory getEmf() {
    return emf;
  }
}

异常(exception)是:

Initial SessionFactory creation failed.javax.persistence.PersistenceException: No   Persistence provider for EntityManager named jspWithJpa
Mar 2, 2014 11:16:08 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.action.InsertData] in context with path   [/jspWithJpa] threw exception [Servlet execution threw an exception] with root cause
javax.persistence.PersistenceException: No Persistence provider for EntityManager named jspWithJpa

最佳答案

persistence.xml 中的提供商标记更改为:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

从您的库中我可以看到您正在使用 eclipselink 作为 JPA 提供程序,而不是 hibernate。

关于java - 初始 SessionFactory 创建失败。javax.persistence.PersistenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124498/

相关文章:

java - 想要使用实体修改 Hibernate 条件

java - 带有 @OneToMany 的随机 LazyInitializationException

java - 字符串对象和堆

java - 在搜寻检视中搜寻,与项目不符

java - Linux 版本的 Eclipse 在尝试打开二进制文件(扩展名为 .bin)时卡住

java - 在 portlet 中设置 SearchContainer 以在使用 EL 和 JSTL 的 JSP 中使用它

java - Spring MVC 中从服务器端发布数据

jsp - JSTL LocalDateTime 格式

javascript - Chrome 57+ 上的 RichFaces 应用程序出现 JS 错误 "Cannot read property ' switchToItem' 未定义”

java - 如何在@Query注解中检索对象成员