Org.dom4j.DocumentException: hibernate.org Nested exception: hibernate.org

I am new to hibernate. I am developing a struts application where I want to integrate hibernate. I am using SQL Server 2008 R2 as the database. I set up my hibernate config file and used annotation based objects. When I try to run my hibernate configuration file, I get this below error:

Error

12:49:41.752 [main] DEBUG org.hibernate.util.DTDEntityResolver - trying to resol
ve system-id [http://hibernate.org/dtd/hibernate-configuration-3.0.dtd]
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could n
ot parse configuration: hibernate.cfg.xml

I gave my files below:

DAOImpl

package com.myProj.dao.impl;

import java.util.logging.Logger;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.myProj.dao.StudentDetailsDAO;
import com.myProj.entity.StudentDetails;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class StudentDetailsDAOImpl implements StudentDetailsDAO{

    Logger LOGGER;
    @SessionTarget
    Session session;
    @TransactionTarget
    Transaction transaction;
    @Override
    public void saveOrUpdateStudentDetail(StudentDetails studDetails) {
        try{
            session.saveOrUpdate(studDetails);
        }catch(Exception e){
            transaction.rollback();
            LOGGER.info("StudentDetailsDAOImpl : saveOrUpdateStudentDetail : Exception "+e.toString());
        }

    }

}

StudentDetails.java

package com.myProj.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="dbo.student_details")
public class StudentDetails {
@Id
@GeneratedValue
@Column(name="id")
private int id;

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

@Column(name="grade")
private String grade;

@Column(name="dob")
private String dob;

@Column(name="stud_address")
private String stud_address;

@Column(name="stud_language")
private String stud_language;

@Column(name="student_marks_id")
private StudentMarks studentMarks;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getGrade() {
    return grade;
}

public void setGrade(String grade) {
    this.grade = grade;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public String getStud_address() {
    return stud_address;
}

public void setStud_address(String stud_address) {
    this.stud_address = stud_address;
}

public String getStud_language() {
    return stud_language;
}

public void setStud_language(String stud_language) {
    this.stud_language = stud_language;
}

public StudentMarks getStudentMarks() {
    return studentMarks;
}

public void setStudentMarks(StudentMarks studentMarks) {
    this.studentMarks = studentMarks;
}
}

StudentMark.java

package com.myProj.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="dbo.student_marks")
public class StudentMarks {
    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

@Column(name="english")
private int english;

@Column(name="physics")
private int physics;

@Column(name="chemistry")
private int chemistry;

@Column(name="biology")
private int biology;

@Column(name="maths")
private int maths;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getEnglish() {
    return english;
}
public void setEnglish(int english) {
    this.english = english;
}
public int getPhysics() {
    return physics;
}
public void setPhysics(int physics) {
    this.physics = physics;
}
public int getChemistry() {
    return chemistry;
}
public void setChemistry(int chemistry) {
    this.chemistry = chemistry;
}
public int getBiology() {
    return biology;
}
public void setBiology(int biology) {
    this.biology = biology;
}
public int getMaths() {
    return maths;
}
public void setMaths(int maths) {
    this.maths = maths;
}

}

HibernateUtil.java

package com.myProj.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.myProj.entity.StudentDetails;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(StudentDetails.class);



        config.configure("hibernate.cfg.xml");
    //new SchemaExport(config).create(true,true);

        sessionFactory = config.buildSessionFactory();

    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.url"> jdbc:sqlserver://<myIp>:1433;databaseName=<myDatabase></property>
        <property name="hibernate.connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="com.myProj.entity.StudentDetails" ></mapping>
        <mapping class="com.myProj.entity.StudentMarks" ></mapping>
    </session-factory>
</hibernate-configuration>

I have provided below a list of jar files that I use:

commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-digester-2.1.jar
hibernate-3.5.3.jar
javax.servlet-3.0.jar
logback-classic-0.9.6.jar
logback-core-0.9.6.jar
ognl-3.0.6.jar
slf4j-api-2.0.99.jar
slf4j-log4j13-1.0.1.jar
sqljdbc-1.2.0.jar
struts-core-1.3.10.jar
struts-taglib-1.3.10-sources.jar
struts-taglib-1.3.5.jar
struts2-core-2.3.8.jar
struts2-json-plugin-2.3.8.jar
struts2-tiles-plugin-2.1.8.1.jar
xwork-2.0.4.jar
xwork--2.3.12.jar

, Apache, :

Apache

java.lang.ExceptionInInitializerError
    at com.myProj.util.HibernateUtil.<clinit>(HibernateUtil.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3744)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4252)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1586)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1212)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:107)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1520)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1194)
    at com.myProj.util.HibernateUtil.<clinit>(HibernateUtil.java:19)
    ... 21 more
Caused by: org.dom4j.DocumentException: hibernate.org Nested exception: hibernate.org
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1576)
    ... 26 more
+6
4

, . , .

<!DOCTYPE hibernate-configuration  PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 <!DOCTYPE hibernate-configuration SYSTEM "classpath://com/myProj/util/hibernate-configuration-3.0.dtd">

hibernate-configuration-3.0.dtd com.myProj.util.Now, , .

+2

jar, :

commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-digester-2.1.jar
hibernate-3.5.3.jar
javax.servlet-3.0.jar
logback-classic-0.9.6.jar
logback-core-0.9.6.jar
ognl-3.0.6.jar
slf4j-api-2.0.99.jar
slf4j-log4j13-1.0.1.jar
sqljdbc-1.2.0.jar
struts-core-1.3.10.jar
struts-taglib-1.3.10-sources.jar
struts-taglib-1.3.5.jar
struts2-core-2.3.8.jar
struts2-json-plugin-2.3.8.jar
struts2-tiles-plugin-2.1.8.1.jar
xwork-2.0.4.jar
xwork-core-2.3.12.jar

, - .

  • , (, , , , ), Struts 1;
  • : Struts2 , 2.3.8 2.3.12 2.1.8.1... what-the-heck?
  • (2.3.32), , , , . , , .

Maven, , , cum grano salis, , lsit :

        struts2-core-2.3.32.jar
          xwork-core-2.3.32.jar
struts2-tiles-plugin-2.3.32.jar
 struts2-json-plugin-2.3.32.jar

                ognl-3.0.19.jar
          freemarker-2.3.22.jar

             log4j-core-2.3.jar
       log4j-slf4j-impl-2.3.jar <-- optional
           slf4j-api-1.7.25.jar <-- optional

, (asm3.3m, asm5.0.2 ..). , Maven 2.3.32, ; ant ( - ), .

, , Hibernate .


, , Java EE (Jboss, Wildfly, Weblogic, Glassfish, TomEE), Hibernate/JPA2 (javax.persistence) JPA2.

JPA2 Java EE 6+. JSR-317 (JPA 2.0) JSR-338 (JPA 2.1), . , , (, Hibernate).

javax.persistence, org.hibernate, JPA2, Hibernate.

, (, ) : ( , ), EntityManager @PersistenceContext EntityManagerFactory.


1: , :

@Column(name="the_name") // needed
private String name;

//@Column(name="name") // not needed
@Column                // this is enough
private String name;

2: camelCase snake_case persistence.xml ( JPA hibernate.cfg.xml, ), fullName full_name @Column.

+2

hibernate.cfg.xml

<property name="hibernate.connection.url">
jdbc:sqlserver://<myIp>:1433;databaseName=<myDatabase>
</property>

<myIp> <myDatabase>

0

As StanislavL mentioned, the problem is how the properties [myIp, myDatabase] are referred to as xml tags. This throws an xml parsing exception.

since you are not using spring, your best bet is to set properties at runtime. refer to these links - hibernate.cfg.xml - Set parameters from the properties file

check the answer "You can do it programmatically" - How to include properties from an external file in the hibernate.cfg.xml file?

0
source

Source: https://habr.com/ru/post/1017013/


All Articles