You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JPA应用中Persistence.createEntityManagerFactory连接MySQL报错及解决

JPA连接MySQL报错问题分析与解决方案

Hey there, let's tackle this issue you're facing with your JPA + Hibernate app connecting to MySQL. That NoClassDefFoundError for org.dom4j.DocumentException is actually a compatibility mismatch between the Hibernate version you're using and the JPA 2.1 declaration in your persistence.xml.

报错详情

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:153)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:78)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at com.javawebtutor.JPATest.main(JPATest.java:13)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

你的配置与测试代码

原persistence.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <persistence-unit name="ProjekatV2" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.javawebtutor.Pacijent</class>
    <class>com.javawebtutor.Intervencija</class>
    <class>com.javawebtutor.BolnickaSoba</class>
    <class>com.javawebtutor.MedicinskiRadnik</class>
    <class>com.javawebtutor.Smena</class>
    <class>com.javawebtutor.Vozac</class>
    <class>com.javawebtutor.Vozilo</class>
    <class>com.javawebtutor.Zahtev</class>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:33060/infosys15495" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="root" />
    </properties>
  </persistence-unit>
</persistence>

测试类JPATest.java

package com.javawebtutor;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPATest {
    private static EntityManager em;

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProjekatV2");
        em = emf.createEntityManager();
        createPacijent("Sladjana", "Nikolic", 1276948354);
        em.close();
        emf.close();
    }

    private static void createPacijent(String name, String surname, long brknji) {
        em.getTransaction().begin();
        Pacijent pac = new Pacijent(name, surname, brknji);
        em.persist(pac);
        em.getTransaction().commit();
    }
}

解决方案

The fix is simple—just update the header of your persistence.xml to use JPA 2.0 instead of 2.1, which matches the compatibility requirements of the older Hibernate EJB3 provider you're using (org.hibernate.ejb.HibernatePersistence):

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

原因说明

The org.hibernate.ejb.HibernatePersistence provider comes from older Hibernate versions (pre-5.x) that don't fully support JPA 2.1. Switching to the JPA 2.0 schema aligns your configuration with the provider's capabilities, resolving the missing dom4j class dependency that was causing the error.

内容的提问来源于stack exchange,提问作者Hazaaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:03:08