当前位置:编程学习 > JAVA >>

Hibernate Junit测试的时候报错问题

如果我联网的话,junit就不会报错,一切正常,如果不联网,出现一下错误:

org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1011)
at xg.myc.model.Uni_Test.beforeClass(Uni_Test.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.dom4j.DocumentException: Server returned HTTP response code: 400 for URL: http://111.11.52.242:8888?wlanacname=1001.0311.311.00&wlanuserip=183.196.20.105 Nested exception: Server returned HTTP response code: 400 for URL: http://111.11.52.242:8888?wlanacname=1001.0311.311.00&wlanuserip=183.196.20.105
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1532)
... 21 more
怎么解决??具体点
谢谢 --------------------编程问答-------------------- 楼主呀,这是加载你的hibernate.cfg.xml的问题哦。

你不联网就出错,联网就不出错,那肯定就是DTD的问题,因为默认写的那些DTD的字符串,是要IDE通过网络去往上加载DTD文件的,这个你可以把这个DTD文件在本地自己给它加载掉,具体的设置是在eclipse的Window->preference->xml->xml catalog这里面去设置进去。。

具体的步骤,你可以百度一下xml catalog设置。 --------------------编程问答-------------------- 肯定是那个配置文件有问题,估计应该是格式不正确吧,可以到网上找个模版试试 啊 --------------------编程问答-------------------- 配置文件是下载的Hibernate压缩包里面拷出来的,应该很标准的啊,你看看,你有不对劲的地方

hibernate.hbm.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

        <!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>

        <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property>  -->

        <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property> 

        <!-- Disable the second-level cache  -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>


        <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
   <property name="hbm2ddl.auto">create</property>  

 
 
    <mapping class="xg.myc.model.User"/>
<mapping class="xg.myc.model.Group"/>

<!--
<mapping resource="xg/myc/model/User.hbm.xml"/>
<mapping resource="xg/myc/model/Group.hbm.xml"/>
--> 
</session-factory>
</hibernate-configuration>



user.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="xg.myc.model">
<class name="User">
<id name="id">
<generator class="native"></generator><!-- 根据数据库的类型自动选择对应生成ID的策略 -->
</id>
    <property name="name"/>
    <many-to-one name="group" column="group_id"></many-to-one>
</class>

</hibernate-mapping>

--------------------编程问答-------------------- hibernate.cfg.xml配置文件的问题 --------------------编程问答-------------------- 看来已经解决了。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,