1

When I tried my first hibernate program and I got this error.

My program simply insert empID,first_name,second_name(table names) and using MySQL database.

Tried lot of solutions sill getting this error.

mapping file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">anandhu</property>

<property   name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update </property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Configuration file

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

 <hibernate-mapping>  
  <class name="Employee" table="emp">  
    <id name="empID" column="empID" type="int">  
     <generator class="assigned"></generator>  
    </id>  

    <property name="firstName" column="first_name" type="string">
    <property name="lastName" column="second_name" type="string">

  </class>  

 </hibernate-mapping>  

This is my persistence class

public class Employee {
private int empID;
private String firstName;
private String lastName;
public int getID(){
    return empID;
}
public void setID(int empID){
    this.empID = empID;
}
public String getFirstName(){
    return firstName;
}
public void setFirstName(String firstName){
    this.firstName = firstName;
}
public String getLastName(){
    return lastName;
}
public void setLastName(String lastName){
    this.lastName = lastName;
}


}

This is my test class

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;



public class EmployeeTest {

public static void main(String[] args) {
    Configuration cf = new Configuration();
    cf.configure("employee.cfg.xml");
    SessionFactory factory = cf.buildSessionFactory();
    Session session = factory.openSession();
    Employee employee = new Employee();
    employee.setID(30);
    employee.setFirstName("raju");
    employee.setLastName("ryan");
    Transaction transaction = session.beginTransaction();
    session.save(employee);
    System.out.println("updated");
    transaction.commit();
    session.close();
    factory.close();


}

}

stack trace

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: employee.cfg.xml
            at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
            at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
            at com.anandhu.hibernateproject.EmployeeTest.main(EmployeeTest.java:16)
        Caused by: org.dom4j.DocumentException: Error on line 3 of document  : 
    The processing instruction target matching "[xX][mM][lL]" is not allowed. 
    Nested exception: 
    The processing instruction target matching "[xX][mM][lL]" is not allowed.
            at org.dom4j.io.SAXReader.read(SAXReader.java:482)
            at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
            ... 2 more
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
maman
  • 107
  • 1
  • 1
  • 6

1 Answers1

2

The problem with this lines

<property name="firstName" column="first_name" type="string">
<property name="lastName" column="second_name" type="string">

Should be

<property name="firstName" column="first_name" type="string" />
<property name="lastName" column="second_name" type="string" />

It would be more simply to find a solution if you show all stacktrace.

You can find examples of working with a configuration and a session here.

Upated

I check this mapping and it works fine. Looks like you have incorrect symbols (may be spaces) in the xml header. May be this will be helpfull Error: The processing instruction target matching “[xX][mM][lL]” is not allowed.

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • I have added the stack trace please check . – maman Dec 03 '15 at 14:20
  • @maman I have tested your configuration with Hibernate 5.0.3. It works fine. Which version of Hibernate do you use? And, please, show all stacktrace. Why do you show only a part? I can't do forecasts. – v.ladynev Dec 04 '15 at 06:48