无法识别的JPA persistence.xml XSD版本:``-休眠,java ee和postgresql

问题描述 投票:0回答:1

[尝试使用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个回答
0
投票

PersistenceException

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

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

有关persistence.xml]中EntityManager和事务类型JTARESOURCE_LOCAL之间的区别的更多信息,请参见this answer


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

在您的persistence.xml

  1. 我在末尾</persistence>之后看不到持久性封闭标签 </persistence-unit>
  2. 您正在使用旧URL
  3. 作为XSD架构位置,将<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">
    

Oracle中的信息:

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

© www.soinside.com 2019 - 2024. All rights reserved.