1

I'm unable to parse hibernate.cfg.xml and geting

Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.hibernate.org/xsd/orm/cfg", local:"hibernate-configuration"). Expected elements are <{}hibernate-configuration> 

It can be something with xml parser, it seams like it uses stax under the hood. We are on hibernate 5.2.3. The header of our hibernate.cfg.xml file is correct I tried both Xsd

 <?xml version="1.0" encoding='utf-8'?> <hibernate-configuration xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <session-factory> 

and DTD

 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> 

Both have the same error. I must be something with the parser.

UPDATE I have fixed the question code was not properly visible. I'm sure the XML is valid XML.

UPDATE 2 I start to get some clues. It can be caused by running the code as junit powermock test. Specifically com.sun.xml.internal.stream.XMLEventReaderImpl class reading the file, is loaded by MockClassLoader.

UPDATE 3 They solved it here by taming powermock a little b

@PowerMockIgnore({ "javax.xml.*", "org.xml.sax.*" }) 

Now I have different error hope I'm on the right track.

java.lang.LinkageError: loader constraint violation: when resolving overridden method "com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Lorg/w3c/dom/Node;)Ljava/lang/Object;" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, com/sun/xml/internal/bind/v2/runtime/unmarshaller/UnmarshallerImpl, and its superclass loader (instance of <bootloader>), have different Class objects for the type org/w3c/dom/Node used in the signature 

That is resolved here by adding additional classes to the

@PowerMockIgnore({ "javax.xml.*", "org.xml.sax.*", "com.sun.xml.*", "com.sun.org.*"}) 
5
  • can you check somewhere in the config file, have you missed < or > symbol Commented Nov 29, 2017 at 14:38
  • also, look into this post stackoverflow.com/questions/5203312/… Commented Nov 29, 2017 at 14:39
  • Please, add your hibernate.cfg.xml, at least part. Commented Nov 29, 2017 at 15:16
  • Did you solve this issue? I'm experiencing this too (using Java 9, if that matters). Commented Mar 12, 2018 at 16:37
  • The update 3 fixed my error completely. Have you tried that? Or do you have other issues? Commented Mar 13, 2018 at 8:53

3 Answers 3

0

can you try to change and use this template for hibernate.cfg.xml file:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="domain"> <property name="hibernate.show_sql">false</property> <mapping package="com.example.dao.entity"/> <!-- etc --> </session-factory> </hibernate-configuration> 
Sign up to request clarification or add additional context in comments.

Comments

0

This error was raised in Hibernate 5.4.4 while using java version "14.0.1".

unexpected element (uri:"http://www.hibernate.org/xsd/orm/cfg", local:"hibernate-configuration"). Expected elements are <{}hibernate-configuration>

A simple downgrade to java 8 resolve it.

As mentioned in the documentation the Hibernate up to 5.4.4. supports Java 8 or 11

Comments

0

I was having the same issue but the fix was simple. I added the correct default namespace to the hibernate.cfg.xml file.

<?xml version="1.0" encoding="UTF-8"?> <hibernate-configuration xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.hibernate.org/xsd/hibernate-configuration"> <session-factory> </session-factory> </hibernate-configuration> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.