Hibernate cannot parse the configuration file solution when the network is not connected

Source: Internet
Author: User

The hibernate framework is often used to perform database operations during the project. However, when the network conditions are poor or the network is not connected, you can add, delete, modify, and query the database, the following exceptions are always reported:

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xmlat org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1418)at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)at org.wjp.hibernate.ExportDB.main(ExportDB.java:8)Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.orgat org.dom4j.io.SAXReader.read(SAXReader.java:484)at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1408)... 3 more

I did not care about this before, but I checked some information to find out the cause of the problem.

Let's take a look at the following code at the beginning of the hibernate. cfg. xml file:

<?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"

The starting part of the common xx. hbm. xml file code is as follows:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

The reason is that it appears in the dtd file, because the two configuration files of hibernate will be searched by default for the above two bold websites, so when you use hibernate for database operations, if your

If the network environment is poor or the network is not connected, the configuration file cannot access the specified dtd authentication file. Therefore, the xml cannot be parsed.

So the solution to this problem is: in a project that requires hibernate, a folder is usually created in this project to store the file for hibernate verification. cfg. xml dtd file and verification xx. hbm. the xml dtd file, for example, the following code, I created a dtd folder under my project, which containsHibernate-configuration-3.0.dtdFile andHibernate-mapping-3.0.dtdFile:

Xx. hbm. xml code: <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN ""Dtd/hibernate-mapping-3.0.dtd"> Hibernate. cfg. xml code: <? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN ""Dtd/hibernate-configuration-3.0.dtd">

When hibernate is used in a networked or non-networked environment, it will go to the path you specified to find the corresponding dtd, instead of going to the hibernate official website to find the corresponding dtd, in this way, the above xml cannot be parsed again.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.