异常:无法解析配置:hibernate.cfg.xml [英] Exception:Could not parse configuration: hibernate.cfg.xml

查看:102
本文介绍了异常:无法解析配置:hibernate.cfg.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我正在尝试做hibernate日志记录。这是我的hibernate.cfg.xml文件

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

< hibernate-configuration>

< session-factory>
< property name =hbm2ddl.auto>更新< / property>
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>
< property name =connection.url> jdbc:mysql:// localhost:3306 / temp< / property>
< property name =connection.username> root< / property>
< property name =connection.password> root< / property>
< property name =connection.driver_class> com.mysql.jdbc.Driver< / property>
< mapping resource =employee.hbm.xml/>
< / session-factory>
< / hibernate-configuration>

以下是例外情况...

  log4j:WARN记录器(org.hibernate.cfg.Environment)中找不到appender。 
log4j:WARN请正确初始化log4j系统。
线程main中的异常org.hibernate.HibernateException:无法解析配置:hibernate.cfg.xml
在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
在org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
在com.subhas.mypackage.StoreData.main(StoreData.java:13)
引起:org.dom4j。 DocumentException:连接被拒绝:连接嵌套异常:连接被拒绝:在org.dom4j.io.SAXReader.read(SAXReader.java:484)处连接
org.hibernate.cfg.Configuration.doConfigure处的
.java:1481)
... 2 more

这是员工。 hbm.xml

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

< hibernate-mapping>
< class name =com.subhas.mypackage.Employeetable =emp1000>
< id name =id>
< generator class =assigned>< / generator>
< / id>

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

< / class>

< / hibernate-mapping>

这是我的主要课程

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

//创建配置对象
配置cfg =新配置();
cfg.configure(hibernate.cfg.xml);

//创建会话工厂对象
SessionFactory factory = cfg.buildSessionFactory();


//创建会话对象
会话会话= factory.openSession();


//创建交易对象
交易记录= session.beginTransaction();

Employee employee = new Employee();

employee.setId(5000);
employee.setfName(Subhas);
employee.setlName(Gupta);


//坚持对象
session.persist(employee);

//提交交易对象
transaction.commit();
session.close();

System.out.println(成功保存的数据);




$ b

我无法找到一个完美的解决方案。有人可以帮助我吗?谢谢。

解决方案

尝试将DOCTYPE更改为此:

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


Actually I am trying to do hibernate logging. This is my hibernate.cfg.xml 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="hbm2ddl.auto">update</property>  
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
            <property name="connection.url">jdbc:mysql://localhost:3306/temp</property>  
            <property name="connection.username">root</property>  
            <property name="connection.password">root</property>  
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <mapping resource="employee.hbm.xml"/>  
        </session-factory>   
    </hibernate-configuration>  

Here is the exception...

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: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at com.subhas.mypackage.StoreData.main(StoreData.java:13)
Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 2 more

this is 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.subhas.mypackage.Employee" table="emp1000">  
        <id name="id">  
         <generator class="assigned"></generator>  
        </id>  

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

      </class>  

     </hibernate-mapping>  

this is my main class

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

    //creating configuration object
    Configuration cfg=new Configuration();
    cfg.configure("hibernate.cfg.xml");

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


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


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

     Employee employee=new Employee();

     employee.setId(5000);
     employee.setfName("Subhas");
     employee.setlName("Gupta");


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

     //commit the transaction object
     transaction.commit();
     session.close();

     System.out.println("Data Saved Successfully");


    }

  }

I am unable to find a perfect solution for this. Could someone help me.? Thanks.

解决方案

Try changing the DOCTYPE to this:

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

这篇关于异常:无法解析配置:hibernate.cfg.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆