Search code examples
javahibernateormhibernate-mapping

Getting NPE while using <cache usage=“read-only”/> in HBMXML in Hibernate 5.2.11


I have upgraded the Hibernate version from Hibernate ORM 4.3 to 5.2.11 version, we are using the hibernate framework using .hbmxml files. We came across a use-case which is throwing NPE now, its a simple use-case of using cache usage attribute in Entity Definition.

The simplified version of our hbmxml file looks as shown below

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class entity-name="CountryCrash" lazy="true"
        name="xyz.test.CountryCrash" table="COUNTRY">
        <cache usage="read-only"/>
        <id name="id" type="long" unsaved-value="0">
            <column name="ID"/>
            <generator class="sequence">
                <param name="sequence">GENERAL_SEQUENCE</param>
            </generator>
        </id>
        <property name="name" type="string">
            <column name="NAME"/>
        </property>
    </class>
</hibernate-mapping>

While this was working perfectly fine with the older versions of Hibernate, with the upgrade to Hibernate Version 5.2.11, it has started throwing the following exception

java.lang.NullPointerException
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXConnector$1.getPublicId(StAXConnector.java:101)
    at org.apache.xerces.util.SAXLocatorWrapper.getPublicId(Unknown Source)
    at org.apache.xerces.xni.parser.XMLParseException.<init>(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
    at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:101)
    at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:75)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(StAXEventConnector.java:261)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:130)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:460)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:435)
    at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:171)
    at org.hibernate.boot.jaxb.internal.MappingBinder.doBind(MappingBinder.java:61)
    at org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:102)
    at org.hibernate.boot.jaxb.internal.AbstractBinder.bind(AbstractBinder.java:84)
    at org.hibernate.boot.jaxb.internal.JaxpSourceXmlSource.doBind(JaxpSourceXmlSource.java:29)
    at org.hibernate.boot.MetadataSources.addDocument(MetadataSources.java:409)
    at org.hibernate.cfg.Configuration.addDocument(Configuration.java:462)

I tried to debug using Hibernate ORM sources but didn’t get much help as to why we are getting an NPE for this scenario.

Please let me know if i am missing something or if we missed something while upgrading. This issue happens only if we add

<cache usage="read-only"/>

attribute in hbmxml, if we remove it, it works fine.

Appreciate your help in Advance.

Thanks,


Solution

  • So I got the issue, the problem with the xml generated is that the cache node should be after tuplizer node as the schema that is being used is http://hibernate.org/xsd/orm/hbm/legacy-mapping-4.0.xsd , which needs the cache node to come after Tuplizer, I got this resolved from the hibernate forums. The discussions details are @ https://discourse.hibernate.org/t/getting-npe-while-using-cache-usage-read-only-in-hbmxml-in-hibernate-5-2-11/4730