javax.persistence.PersistenceException - JPA+Hibernate

杀马特。学长 韩版系。学妹 提交于 2019-11-27 23:23:20

The issue is a mismatch between your <persistence version="2.1"...> and your hibernate library requirements. Copy and paste the whole persistence tag with name spaces (xmlns) from a reliable sample. Then check your library version, below.

From hibernate.org: http://hibernate.org/orm/downloads/

Supported JPA Versions

JPA 1.0: ORM 3.2+
JPA 2.0: ORM 3.5+
JPA 2.1: ORM 4.3+

Note that newer ORM releases are backwards compatible with older JPA versions (ex: ORM 4.3 with JPA 1.0). However, newer ORM releases may not be compatible with older JPA containers.

The JPA version is the same as the persistence version. The ORM is the version of hibernate. So your persistence file has <persistence version="2.1" ... >. So, as of now, you want only the hibernate library and version below (or a later version):

WEB-INF/lib/hibernate-entitymanager-4.3.0.Final.jar

or Maven pom.xml:

<!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.0.Final</version>
</dependency>

I spent a whole bunch of hours on this one. I hope my time spent saves yours!

I suspect your environment only supports persistence 2.0. Try changing:

<persistence version="2.1" 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">

To:

<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_0.xsd" version="2.0">

you are missing the <persistence> element in your xml... check the xsd you posted and make your persistence.xml comply to the schema. See this link for a simple example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!