3

我是休眠新手,在处理一个示例时,我遇到了以下错误:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.tieto.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
    at com.tieto.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.tieto.Hib.main(Hib.java:9)
Caused by: 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 org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    at com.tieto.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more
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)
    ... 5 more

我的 hibernate.cfg.xml 文件是

<?xml version="1.0"?>
<!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">abc</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <mapping resource="com/abc/Test.hbm.xml" />
</session-factory>
</hibernate-configuration>

请提出我在这方面做错了什么?

4

4 回答 4

1

从hibernate文件夹中的hibernate-mapping-3.0.dtdhibernate-configuration-3.0.dtd复制dtd (路径:hibernate3/org/hibernate/hibernate-mapping-3.0.dtd和hibernate3/org/hibernate/hibernate-configuration -3.0.dtd) 并将其粘贴到您的 hbm.xml 文件和 cfg.xml 文件中。

于 2014-06-27T04:18:33.533 回答
0

you must first, create a file with this name "log4j.properties" (and in a maven project put it in the "resources" folder - subfolder of main folder-) then put this slice of code in pom.xml:

<build>
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>${basedir}/target</targetPath>
        <includes>
            <include>log4j.properties</include>
        </includes>
    </resource>
</resources>

于 2014-02-08T11:28:26.600 回答
0

原因:org.dom4j.DocumentException:连接被拒绝:连接嵌套异常:连接被拒绝:连接在 org.dom4j.io.SAXReader.read(SAXReader.java:484) 在 org.hibernate.cfg.Configuration.doConfigure(Configuration.爪哇:1481)... 5更多

根据堆栈跟踪的最后几行,我认为问题与您的数据库连接有关。

请检查您要连接的数据库的名称(test)、用户名(root)和密码。(abc)

于 2013-07-01T11:24:27.770 回答
0

请检查下面列出的所有必需的 jar 文件,

  1. antlr-2.7.7.jar
  2. commons-lang3-3.1.jar
  3. commons-logging-1.1.3.jar
  4. dom4j-1.6.1.jar
  5. hibernate-commons-annotations-4.0.1.Final.jar
  6. hibernate-core-4.2.0.Final.jar
  7. hibernate-jpa-2.0-api-1.0.1.Final.jar
  8. javassist-3.15.0-GA.jar
  9. jboss-logging-3.1.0.GA.jar
  10. jboss-transaction-api_1.1_spec-1.0.0.Final.jar

您看起来最关心与日志相关的 jar。

于 2013-07-01T10:40:10.900 回答