Our project use's Hibernate's programmatic Configuration
to set up our SessionFactory and such. I just migrated us from version 3 to version 4 of Hibernate. Now I am getting the error "Element type "hibernate-mapping" must be declared." which it says is a SaxParseException. That's great and all, but I checked my WEB-INF/lib directory and Hibernate's version 4 core .jar file is there, so it's on the classpath.
At first I thought it's because 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 that doesn't fix the error. What is going on? In a separate project where I am using the XML configuration file for Hibernate, I did the same migration, and it went fine. Please note that on my environment classpath must be used, the DTD cannot be downloaded from the internet or anything like that. It shouldn't be anyway.
edit: here's the exception as requested:
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)
I also just migrated from 3.0 to 4.0, I assume 3 causes I use the following DTD's
THE ACTUAL FIX IN THIS CASE
Make sure that you dont have any old 3.0 jar's in the path, else you can see this exception.
Possible Cause 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 the 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 Cause 2
You have misspelt <hibernate-mapping>
in your hbm file.
Edit :
I am using mixed configuration both programmatic and cfg files. When I tried to use all programmatic, it did not work for me. Nor did I get much help from SO. But the 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 it.
Overall Advice
Going all programmatic is a bad idea. Since there is a lot of programmatic stuff you need to add from column to variable mapping to variable type. It will be a debugging nightmare. I suggest doing non programmatic stuff for things that you can do without programmatic. For me I just needed to get the username password from cmd line, so that I can deploy the product on any server. So I just made that programmatic.
I also migrate from hibernate 3 to 4 ,
For hibernate.cfg.xml file i use following DTD
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
For mapping file i use follwing DTD:
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
If i change dtd it gives exception because hibernate 4 use xsd instead of dtd. Hibrenate Jira of migrating dtd to xsd
So you have to use xsd file instead of dtd.
Hibernate hbm example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With