【问题标题】:Unrecognized JPA persistence.xml XSD version : `` - hibernate, java ee and postgresql无法识别的 JPA persistence.xml XSD 版本:``-hibernate、java ee 和 postgresql
【发布时间】:2019-12-15 03:30:11
【问题描述】:

尝试使用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 然后连接到数据库(在本地主机上)并执行一些查询,那就太好了... 谢谢!

【问题讨论】:

    标签: postgresql java-ee-7 hibernate-entitymanager


    【解决方案1】:

    PersistenceException

    如果您在 JavaEE 环境中使用 EntityManagerFactory,您需要在 persistence.xml 中将 transaction-type 定义为 RESOURCE_LOCAL

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

    有关 persistence.xmlEntityManager 和事务类型JTARESOURCE_LOCAL 之间的区别的更多信息,请参阅this answer


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

    在您的 persistence.xml 中:

    1. 我没有看到末尾的&lt;/persistence-unit&gt; 之后的持久性封闭标记 &lt;/persistence&gt;
    2. 您正在为 XSD 架构位置 使用 旧网址,请将您的 &lt;persistence ... &gt; 更改为:
    <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">
    

    来自Oracle的信息:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2015-05-19
      相关资源
      最近更新 更多