postgresql - 无法识别的 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 然后连接到数据库(在本地主机上)并执行一些查询,那就太好了......谢谢!
解决方案
持久性异常:
如果EntityManagerFactory
在 JavaEE 环境中使用,则需要在persistence.xml中定义事务类型:RESOURCE_LOCAL
<persistence-unit name="mypersistenceunit" transaction-type="RESOURCE_LOCAL">
EntityManager
有关事务类型JTA
和persistence.xmlRESOURCE_LOCAL
中的区别和差异的更多信息,请参阅此答案。
无法识别的 JPA persistence.xml XSD 版本:
在您的persistence.xml中:
- 最后我没有看到持久性封闭标签 。
</persistence>
</persistence-unit>
- 您正在为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/
。
推荐阅读
- javascript - 根据值从对象数组中选择一个属性:Javascript
- angular - Angular Material Mat-select with multiple selections:显示所选值的问题
- node.js - 高请求发送时间
- c# - 如何接受带有 selenium firefox 驱动程序的自签名 SSL 证书?
- java - 对实体对象 spring/JPA/Java 进行验证的缺点
- html - 当 Bootstrap 中的内联表单时,如何使标签显示为“大尺寸”?
- c# - 试图将数据字符串从控制器传递到视图
- c# - RevitAPI C#:如何取消停靠 DockablePane onStart
- microsoft-graph-api - 如何在没有登录提示的情况下使用 Microsoft 图形 API
- php - Dompdf - 图像不以 pdf 格式呈现