[尝试使用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,然后连接到数据库(在本地主机上)并执行一些查询,那将非常好...谢谢!
如果在JavaEE环境中使用EntityManagerFactory
,则需要在persistence.xml中将transaction-type定义为RESOURCE_LOCAL
:
<persistence-unit name="mypersistenceunit" transaction-type="RESOURCE_LOCAL">
有关persistence.xml]中EntityManager
和事务类型JTA
与RESOURCE_LOCAL
之间的区别的更多信息,请参见this answer。
在您的persistence.xml:中
</persistence>
之后看不到持久性封闭标签 </persistence-unit>
。<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/
。