Java 异常:无法解析配置:hibernate.cfg.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23681589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:39:46  来源:igfitidea点击:

Exception:Could not parse configuration: hibernate.cfg.xml

javahibernate

提问by Subhas Gupta

Actually I am trying to do hibernate logging. This is my hibernate.cfg.xml file

实际上我正在尝试进行休眠日志记录。这是我的 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="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

这是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.

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

回答by LucasNz

Try changing the DOCTYPE to this:

尝试将 DOCTYPE 更改为:

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

回答by Anand j. Kadhi

I am not sure but i think you are missing BasicConfiguration.configure() for some log4J configuration then you would not face log4J initailization exception.

我不确定,但我认为您缺少某些 log4J 配置的 BasicConfiguration.configure() 那么您就不会面临 log4J 初始化异常。

回答by Prashant Rahate

My friend Error just cause an not proper dialect selected. Please make sure it is correct and you also need to add column property in mapping file.

我的朋友错误只是导致选择了不正确的方言。请确保它是正确的,您还需要在映射文件中添加列属性。

<class name="Employee" table="EMPLOYEE">

    <id name="id" column="ID">
        <generator class="assigned"></generator>
    </id>

    <property name="name" column="NAME"></property>
    <property name="mobile" column="MOBILE"></property>
    <property name="email" column="EMAIL"></property>

</class>

am sure your error get resolved with this...

相信你的错误得到解决...