【问题标题】:Exception in hibernate main [duplicate]休眠主程序中的异常[重复]
【发布时间】:2016-02-09 05:21:06
【问题描述】:
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 文件时出现上述错误。我是休眠新手,所以请帮助我

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");  

}  
}

员工.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**

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

          `      

【问题讨论】:

    标签: java mysql spring hibernate


    【解决方案1】:

    朋友们,我的问题解决了,执行得很好。我对“hibernate.cfg.xml”文件进行了更改。这里是更改

    <?xml version="1.0"?> 
    

    而不是

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 2012-01-07
    • 2018-04-01
    • 2012-12-27
    • 2013-03-25
    • 1970-01-01
    • 2012-09-19
    相关资源
    最近更新 更多