Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't parse hibernate.cfg.xml while offline

Tags:

java

hibernate

Whenever I'm disconnected from the internet, I get the following exception:

org.hibernate.HibernateException: Could not parse configuration: com/mashlife/resources/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)

Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1532)
    ... 45 more

This only happens when I'm offline. Does hibernate try to read the DTD when parsing the config? What's the root cause here?

Here is my hibernate.cfg.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/foo</property>
        <property name="connection.username">user</property>
        <property name="connection.password">pass</property>

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

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

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

        <!-- DO NOT Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <!-- Names the annotated entity class -->
        <!--<mapping class="org.hibernate.tutorial.annotations.Event"/>-->

    </session-factory>

</hibernate-configuration>
like image 967
ripper234 Avatar asked Nov 29 '10 06:11

ripper234


2 Answers

Hibernate can resolve the DTDs locally (without a network connection).

Your DOCTYPE is using the new namespace (http://www.hibernate.org/dtd/) for Hibernate 3.6, so you might have an older version of the Hibernate libraries in your classpath.

I experienced the same issue after upgrading to Hibernate 3.6.8.Final. I had multiple versions of hibernate3.jar on the classpath causing an old incompatible version of the DTD Entity Resolver to be loaded which only works with the old namespace (http://hibernate.sourceforge.net/). For reference, here's a link to the newer DTD Entity Resolver.

I'm using hibernate3-maven-plugin which has a transitive dependency on an older version of Hibernate so I just had to specify a plugin dependency on Hibernate 3.6.8.Final.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
    ...
</configuration>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.8.Final</version>
    </dependency>
</dependencies>
</plugin>
like image 183
JRS Avatar answered Oct 19 '22 01:10

JRS


It is not possible because, hibernate jar files is also load the some dtd content but slow internet connection it is worked.

(1) Hibernate Configuration File Location

The first solution was to provide the DTD file location in the system using classpath. So the DocType that worked offline would be;

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

(2)Use SourceForge DTD URL with SYSTEM

Another solution I found working is when I change the DTD URL to SourceForge and changed the declaration from PUBLIC to SYSTEM.

So below will also work if your system is offline.

<!DOCTYPE hibernate-configuration SYSTEM 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

Hibernate Work Offline

like image 24
Gautam Viradiya Avatar answered Oct 19 '22 00:10

Gautam Viradiya