首页 > 解决方案 > 无法识别的 JPA persistence.xml XSD 版本:``-hibernate、java ee 和 postgresql

问题描述

尝试使用entitiManagerFactory 制作entityManager 时出现此问题。应用程序在 docker 容器内运行,并且 postgresql 数据库在机器的本地主机上(不在 docker 内)。

我的persistence.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_2_1.xsd"
version="2.1">
<!-- Define persistence unit -->
<persistence-unit name="mypersistenceunit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>some.path.SimplifiedUserGroup</class>
    <class>some.path.UserSettings</class>
    <class>some.path.UserGroupSettings</class>
    <class>some.path.UserGroup</class>
    <class>some.path.AppUser</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/localdatabase" />
        <property name="javax.persistence.jdbc.user" value="postgres" />
        <property name="javax.persistence.jdbc.password" value="postgres" />
    </properties>
</persistence-unit>

和存储库类:

public List<SimplifiedUserGroup> findAll() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("mypersistenceunit");
    entityManager = emf.createEntityManager();
    return entityManager.createNamedQuery("UserGroup.findAll", SimplifiedUserGroup.class).getResultList();
}

有一个错误:

javax.persistence.PersistenceException: Unable to locate persistence units

接着:

java.lang.IllegalArgumentException: Unrecognized JPA persistence.xml XSD version : ``

我尝试了几个教程并阅读了 stackoverflow 主题,但没有任何帮助 - 我尝试过但没有帮助。与 2.0、2.1、2.2 版本相同。我的 pom.xml 中有这样的依赖项:

dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.3.6.Final</version>
    </dependency>

如果我可以创建一个 entityManager 然后连接到数据库(在本地主机上)并执行一些查询,那就太好了......谢谢!

标签: postgresqljava-ee-7hibernate-entitymanager

解决方案


持久性异常

如果EntityManagerFactory在 JavaEE 环境中使用,则需要在persistence.xml中定义事务类型RESOURCE_LOCAL

<persistence-unit name="mypersistenceunit" transaction-type="RESOURCE_LOCAL">

EntityManager有关事务类型JTApersistence.xmlRESOURCE_LOCAL中的区别和差异的更多信息,请参阅此答案


无法识别的 JPA persistence.xml XSD 版本

在您的persistence.xml中:

  1. 最后我没有看到持久性封闭标签</persistence></persistence-unit>
  2. 您正在为XSD Schema Location使用旧 URL ,请将您的更改为:<persistence ... >
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  version="2.1">

来自甲骨文的信息:

从 2.1 版本开始,Java Persistence API 模式共享命名空间,http://xmlns.jcp.org/xml/ns/persistence/. 以前的版本使用命名空间http://java.sun.com/xml/ns/persistence/


推荐阅读