6

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)
KyleM
  • 4,445
  • 9
  • 46
  • 78
  • can you post the exception trace please, the SAXParser exception would also say what went wrong.. – Anantha Sharma Jun 14 '13 at 18:17
  • @AnanthaSharma Ok I posted it.. – KyleM Jun 14 '13 at 18:24
  • is this the hibernate.cfg.xml file or hbm.xml file.. the hibernate-mapping entry comes in the hbm.xml files, you can try with this ` ` – Anantha Sharma Jun 14 '13 at 18:40
  • We don't use a hibernate.cfg.xml file, as the original post says we use the Configuration Object. So this is in the hbm.xml file. Why would I try the one you suggested? The Hibernate documentation says the way I'm doing it should work... anyway I will try it I just don't see why that should work... – KyleM Jun 14 '13 at 18:42
  • 1
    Don't know if it helps, but I have the following mapping file that works perfectly: ` `. – acdcjunior Jun 18 '13 at 14:28
  • @acdcjunior Thanks. That's the DTD from prior to version 3.6. Hibernate doc says to use the hibernate.org one now edit: Also, our older files have that one as well which also doesn't work – KyleM Jun 18 '13 at 14:29
  • 1
    See if you have a conflicting version of xerces in you classpath. That DTD declaration should work, so the problem is somewhere else. – acdcjunior Jun 18 '13 at 14:38
  • Please post the entire stack trace. – Siddharth Jun 18 '13 at 14:40
  • @acdcjunior Why would having xerces on my classpath be a conflict? Does the Hibernate v4 have that jar file somewhere? – KyleM Jun 18 '13 at 14:58
  • 1
    @KyleM I said that because I had other jars that had xerces as dependency which ended up messing everyone else that also had xerces as dependency. Anyway, forget about it. If I were you, I'd take a stripping approach: take the jars, one by one, from the classpath, until it works or... you have no jars left. – acdcjunior Jun 18 '13 at 15:07
  • @acdcjunior Agreed, that's what I'll do. And btw I DO have xerces on the classpath. However v4 of Hibernate just doesn't have xerces as a jar so I don't see why it'd screw it up... BUT maybe they renamed it or something. Btw I suggest u post as an answer – KyleM Jun 18 '13 at 15:12
  • @acdcjunior I got rid of xerces.jar as you suggested. I updated to the newest version of xerces. This fixed all of my issues. If you post as an answer (with your suggestion that its jar related, and to specifically look at xerces) within the next few days I will award you the bounty. Otherwise, I will award Siddharth the bounty. – KyleM Jun 18 '13 at 22:30
  • 1
    Hum, I saw this too late. I would have suggested you edited your question with the solution taken (that worked) and asked for an explanation of why `xerces.jar` was messing it all up. I wouldn't feel "worthy" of the bounty because while my comment helped to work around it, we didn't really discover the root cause. – acdcjunior Jun 19 '13 at 22:35
  • @acdcjunior It's not a question of worthiness really. Your comments helped though. I'm also not sure why xerces.jar messed it up because using the "tattletale" utility (which produces a report of conflicting jar classes), xerces didn't seem to have relevant conflicts. So I doubt I will ever know but TBH, I am just happy it's fixed. – KyleM Jun 20 '13 at 18:28

2 Answers2

11

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.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • It's clearly stated in the question that I am not using .cfg.xml, I am using programmatic Configuration ... does the .dtd version from the configuration have to match the .dtd version from the hibernate-mapping? e.g. do they both have to be v3.0? I don't think they do, so I don't see how this is relevant.. DTD just describes syntax/structure of the XML file, right? – KyleM Jun 18 '13 at 14:52
  • Btw, I will look into possible cause 2. Thanks for the post. – KyleM Jun 18 '13 at 14:59
  • I did see SAXException, please continue to hold patience, there are a lot of things that went wrong, and I dont remember what change fixed what exception (frankly I dont care), I am just trying to recall all that I did at that time to fix my issue. Trust me it works, so we will get there. – Siddharth Jun 18 '13 at 15:02
  • 1
    Just noting that the solution was jar related. I removed ALL old hibernate related jar files and started over. I also removed xerces and updated our version of xerces to the newest one. Your edit (everything relating to programmatic configuration), was not relevant to the problem however. I am making this note for others who see this solution. – KyleM Jun 19 '13 at 21:37
1

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

Punit Patel
  • 901
  • 1
  • 12
  • 25
  • 1
    No you don't. This information directly contradicts the Hibernate documentation which says you can still use the 3.0 DTD. Furthermore, I fixed everything yesterday as mentioned in the comments on my post... so everything is now working using the 3.0 DTD. – KyleM Jun 19 '13 at 15:02