我是hibernate的新手,在处理一个例子时,我遇到了以下错误:
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>
请说明我错在哪了??
答案 0 :(得分:1)
从h strongnate文件夹中的 hibernate-mapping-3.0.dtd 和 hibernate-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文件中。
答案 1 :(得分:0)
请检查下面列出的所有必需的jar文件,
您最关心的是日志相关的罐子。
答案 2 :(得分:0)
引起:org.dom4j.DocumentException:连接被拒绝:连接嵌套异常:连接被拒绝:连接 在org.dom4j.io.SAXReader.read(SAXReader.java:484) 在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) ......还有5个
根据堆栈跟踪的最后几行,我认为该问题与数据库的连接有关。
请检查名称(test)
,用户名(root)
&amp;您希望连接到的数据库的密码(abc)
。
答案 3 :(得分:0)
首先,你必须创建一个名为“log4j.properties”的文件(并在maven项目中将它放在“resources”文件夹 - 主文件夹的子文件夹中),然后将这段代码放在pom.xml中:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${basedir}/target</targetPath>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>