This question already has answers here:
Error: The processing instruction target matching “[xX][mM][lL]” is not allowed
                                
                                    (7个答案)
                                
                        
                                4年前关闭。
            
                    
0    [main] INFO  org.hibernate.cfg.Environment  - Hibernate 3.2.1
**strong text**15   [main] INFO  org.hibernate.cfg.Environment  - hibernate.properties not found
**strong text**15   [main] INFO  org.hibernate.cfg.Environment  - Bytecode provider name : cglib
**strong text**46   [main] INFO  org.hibernate.cfg.Environment  - using JDK 1.4 java.sql.Timestamp handling
**strong text**269  [main] INFO  org.hibernate.cfg.Configuration  - configuring from resource: hibernate.cfg.xml
**strong text**269  [main] INFO  org.hibernate.cfg.Configuration  - Configuration resource: hibernate.cfg.xml
**strong text**691  [main] ERROR org.hibernate.util.XMLHelper  - Error parsing XML: hibernate.cfg.xml(1) The processing instruction target matching "[xX][mM][lL]" is not allowed.
**strong text**Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at com.javatpoint.mypackage.StoreData.main(StoreData.java:15)
Caused by: org.dom4j.DocumentException: Error on line 1 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)


我在尝试运行storedata.java file.i时遇到上述错误。

Employee.java

package com.javatpoint.mypackage;
public class Employee {
private int id;
private String firstName,lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
enter code here
}
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;
}
}


StoreData.java

package com.javatpoint.mypackage;



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

   public class StoreData {
   public static void main(String[] args) {

   //creating configuration object
   Configuration cfg=new Configuration();
   cfg.configure("hibernate.cfg.xml");
   //populates the data of             the         configuration file

    //creating seession factory object
    SessionFactory factory=cfg.buildSessionFactory();

//creating session object
Session session=factory.openSession();

//creating transaction object
Transaction t=session.beginTransaction();

Employee e1=new Employee();
e1.setId(115);
e1.setFirstName("sonoo");
e1.setLastName("jaiswal");

session.persist(e1);//persisting the object

t.commit();//transaction is committed
session.close();

System.out.println("successfully saved");

}
}


employee.hbm.xml

<?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="com.javatpoint.mypackage.Employee" table="emp1000">
  <id name="id">
   <generator class="assigned"></generator>
   </id>

<property name="firstName"></property>
<property name="lastName"></property>

 </class>

</hibernate-mapping>


hibernate.cfg.xml

<?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="hbm2ddl.auto">update</property>
property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/SampleDB</property>
        <property name="connection.username">root</property>
        <property name="connection.password">anbu</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="employee.hbm.xml"/>
    </session-factory>

</hibernate-configuration>


log4j.properties

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.

log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.

log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x
-         %m%n**strong text**


我已经为休眠插入了所有必要的jar文件,并正确配置了mysql。

          `

最佳答案

朋友,我的问题解决了,它表现得很好。我在“ hibernate.cfg.xml”文件中进行了更改。

<?xml version="1.0"?>


代替

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

关于java - hibernate 主程序中的异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33594286/

10-11 14:32