问题
I am using JBOSS 5.1.0.GA and Hibernate
when I try to connect to the database I get the following errors
10:21:03,042 INFO [Version] Hibernate Commons Annotations 3.1.0.GA
10:21:03,049 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
10:21:03,049 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
10:21:03,077 INFO [STDOUT] Warning: Caught exception attempting to use SAX to load a SAX XMLReader
10:21:03,077 INFO [STDOUT] Warning: Exception was: java.lang.ClassCastException: org.apache.xerces.parsers.SAXParser cannot be cast to org.xml.sax.XMLReader
10:21:03,077 INFO [STDOUT] Warning: I will print the stack trace then carry on using the default SAX parser
10:21:03,077 ERROR [STDERR] java.lang.ClassCastException: org.apache.xerces.parsers.SAXParser cannot be cast to org.xml.sax.XMLReader
10:21:03,078 ERROR [STDERR] at org.xml.sax.helpers.XMLReaderFactory.loadClass(Unknown Source)
10:21:03,078 ERROR [STDERR] at org.xml.sax.helpers.XMLReaderFactory.createXMLReader(Unknown Source)
10:21:03,078 ERROR [STDERR] at org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:83)
10:21:03,078 ERROR [STDERR] at org.dom4j.io.SAXReader.createXMLReader(SAXReader.java:894)
10:21:03,078 ERROR [STDERR] at org.dom4j.io.SAXReader.getXMLReader(SAXReader.java:715)
10:21:03,078 ERROR [STDERR] at org.dom4j.io.SAXReader.read(SAXReader.java:435)
10:21:03,078 ERROR [STDERR] at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1518)
then
10:21:03,386 INFO [STDOUT] 10:21:03,382 ERROR [DatabaseManager] Error intialising Hibernate
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1528)
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:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
Caused by: org.dom4j.DocumentException: SAX2 driver class org.apache.xerces.parsers.SAXParser does not implement XMLReader Nested exception: SAX2 driver class org.apache.xerces.parsers.SAXParser does not implement XMLReader
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1518)
I have the following dependencies:-
hibernate-annotations 3.4.0.GA hibernate-commons-annotations 3.1.0.GA hibernate-core 3.3.0.SP1
I suspect I have a conflict between a jar in the jboss lib and one in my dependencies. can anyone suggest what it might be?
回答1:
I got about same error when trying to run Hibernate 4.0.0.CR6 in JBoss 5.0.1.
The parser needed by Hibernate to parse configuration files seems to be provided by jboss endorsed libraries (/lib/endorsed/xercesImpl.jar I believe)and hibernate's xml-apis dependency somehow interfered with them.
Not packing xml-apis .jar in generated archive (.war in my case) turned out to fix the problem. To do that I overrode the scope of the dependency in the .pom of the project generating the archive (overriding anywhere else wouldn't do because provided scope is not transitive):
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.0.b2</version>
<scope>provided</scope>
</dependency>
来源:https://stackoverflow.com/questions/3413488/saxparser-errors-with-hibernate-and-jboss-conflicting-versions