Hibernate error, possibly with DTD declaration

Our project uses the Hibernate Configuration software to configure our SessionFactory, etc. I just ported us from version 3 to version 4 of Hibernate. Now I get the error "The element type" hibernate-mapping "must be declared." which, he says, is an exception to SaxParseException. This is great and that’s all, but I checked my WEB-INF / lib directory and the .jar kernel for Hibernate version 4, so it is on the class path.

At first I thought, because the Hibernate team migrated from

 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

to

 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

But this does not fix the error. What's happening? In a separate project where I use the XML configuration file for Hibernate, I did the same migration and everything went fine. Please note that in my environment the classpath must be used, DTD cannot be downloaded from the Internet or anything like that. In any case, it should not be.

edit: here is an exception on request:

 Caused by: org.xml.sax.SAXParseException; systemId: ; lineNumber: 6; columnNumber: 20; Element type "hibernate-mapping" must be declared. at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1213) at org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1807) at org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:3633) at org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:1229) at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:938) at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381) at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098) 
+6
source share
2 answers

I also just switched from 3.0 to 4.0, I assume 3 reasons to use the following DTDs

ACTUAL USE IN THIS CASE

Make sure you don't have the old version 3.0 in the way, otherwise you might see this exception.

Possible reason 1

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

And for hbm files

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

Works well for me.

Possible reason 2

You have misspelt <hibernate-mapping> in your hbm file.

Edit:

I use a mixed configuration of both software and cfg files. When I tried to use all the programs, this did not work for me. I also did not get much help from SO. But below worked for me.

 try { String connection = "jdbc:mysql://" + Globals.DBSERVER.trim() + "/myDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10"; log.debug("Connection URL "+connection) ; Configuration configuration = new Configuration(); configuration .setProperty("hibernate.connection.url", connection) .setProperty("hibernate.connection.username", Globals.DB_USER_NAME.trim()) .setProperty("hibernate.connection.password", Globals.DB_PASSWORD.trim()) ; configuration.configure(); sessionFactory = configuration .buildSessionFactory(new ServiceRegistryBuilder() .applySettings(configuration.getProperties()).buildServiceRegistry()); } catch (Exception e) { log.fatal("Unable to create SessionFactory for Hibernate"); log.fatal(e.getMessage()); log.fatal(e); e.printStackTrace(); } 

My question that helped me fix this.

General advice

Switching to all programs is a bad idea. Since there are many program materials that need to be added from a column to a variable mapping in a variable type. This will be a debugging nightmare. I suggest doing non-software things for things that you can do without software. For me, I just needed to get the user password from the cmd line so that I could deploy the product to any server. So I just made this program.

+10
source

I also move from sleep mode 3 to 4,

For the hibernate.cfg.xml file, I use the following DTD

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

For the mapping file, I use the following expression:

<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

If I modify dtd, it gives an exception because hibernate 4 uses xsd instead of dtd. Hibrenate Jira Transfer dtd to xsd

So you should use the xsd file instead of dtd.

Hibm hibernate example

+1
source

Source: https://habr.com/ru/post/947325/


All Articles