线程“main”中的异常java.lang.NoClassDefFoundError:org / dom4j / io / STAXEventReader

时间:2017-03-17 10:35:48

标签: java hibernate maven jpa dom4j

我正在使用JPA + Hibernate 5.我遇到了dom4j的问题。

非常感谢。

Class Empleado.java - >

@Entity
@Table(name = "EMPLEADO")
public class Empleado implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "COD_EMPLEADO")
    private Long codigo;

    @Column(name = "APELLIDOS")
    private String apellidos;

    @Column(name = "NOMBRE")
    private String nombre;

    @Column(name = "FECHA_NACIMIENTO")
    private Date fechaNacimiento;

    public Empleado() {

    }

    public Empleado(Long codigo, String apellidos, String nombre, Date fechaNacimiento) {

        this.codigo = codigo;
        this.apellidos = apellidos;
        this.nombre = nombre;
        this.fechaNacimiento = fechaNacimiento;
    }

    public Long getCodigo() {
        return codigo;
    }

    public void setCodigo(Long codigo) {
        this.codigo = codigo;
    }

    public String getApellidos() {
        return apellidos;
    }

    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public Date getFechaNacimiento() {
        return fechaNacimiento;
    }

    public void setFechaNacimiento(Date fechaNacimiento) {
        this.fechaNacimiento = fechaNacimiento;
    }

    @Override
    public String toString() {
        return "Empleado [codigo=" + codigo + ", apellidos=" + apellidos + ", nombre=" + nombre + ", fechaNacimiento="
                + fechaNacimiento + "]";
    }



}

Class TestEmpleado: - >

public class TestEmpleados {

    private static EntityManager manager;

    private static EntityManagerFactory emf;

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        emf = Persistence.createEntityManagerFactory("Persistencia");
        manager = emf.createEntityManager();

        List<Empleado> empleados = (List<Empleado>) manager.createQuery("FROM Empleado").getResultList(); 
        System.out.println(empleados.size() + "Empleados");


    }

}

这是我的persistence.xml: - &gt;

<?xml version="1.0" encoding="UTF-8" ?>

<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" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="Persistencia">

    <class>es.makigas.hibernate.modelo.Empleado</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem/test" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />


            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>

</persistence>

最后这是我的POM: - &gt;

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>es.makigas</groupId>
    <artifactId>hibernate-jpa-ejemplo</artifactId>
    <version>1.0.0-SNAPSHOT</version>



    <dependencies>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.0.Final</version>
    </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.191</version>
        </dependency>




    </dependencies>





    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

当我执行TestEmpleados时,我收到以下错误 - &gt;

mar 17, 2017 11:18:07 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: Persistencia
    ...]
mar 17, 2017 11:18:07 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
mar 17, 2017 11:18:07 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
mar 17, 2017 11:18:07 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/io/STAXEventReader
    at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)
    at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:87)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:180)
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:34)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:165)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:114)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at es.makigas.hibernate.tests.TestEmpleados.main(TestEmpleados.java:19)
Caused by: java.lang.ClassNotFoundException: org.dom4j.io.STAXEventReader
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 11 more

2 个答案:

答案 0 :(得分:0)

  

Hibernate核心5.1.0.Final包含dom4j-1.6.1 jar,它会抛出此错误。要解决此问题,需要从hibernate-core中排除dom4j-1.6.1并在你的pom中包含dom4j-1.6。

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.1.0.Final</version>
  <exclusions>
    <exclusion>
        <artifactId>jta</artifactId>
        <groupId>javax.transaction</groupId>
    </exclusion>
    <!-- Exclude SLF4j to avoid version conflicts (we have 1.6.6, this drags 
            in 1.6.1) -->
    <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </exclusion>
    <!-- Exclude dom4j to avoid version conflicts (we have 1.6, this drags 
            in 1.6.1) -->
    <exclusion>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<!-- add dom4j in the correct version -->
<dependency>
  <groupId>dom4j</groupId>
  <artifactId>dom4j</artifactId>
  <version>1.6</version>
</dependency>

答案 1 :(得分:0)

我在pom.xml中看到两个依赖项不匹配 - Hibernate 5.1.0和JPA 1.0.2 如果你想使用Hibernate 5.1.0那么你应该使用JPA 2.1。请进行此更改,它应该可以正常工作。

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.1.0</version>
</dependency>