InvalidMappingException:无法解析映射文档

时间:2015-12-30 11:07:53

标签: java oracle hibernate

我多次尝试运行Simple Hibernate Project,但错误更多。我试图使用Hibernate.cfg.xml文件,但没有成功。 Hibernate.cfg.xml只出现NullPointerException。

堆栈跟踪:

30 12 2015 12:03:49,144  INFO Version:37 - HHH000412: Hibernate Core {5.0.6.Final}
30 12 2015 12:03:49,150  INFO Environment:198 - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.username=DTCUSR, hibernate.connection.password=****, hibernate.dialect=org.hibernate.dialect.Oracle10gDialect, hibernate.connection.url=jdbc:oracle:thin:@ORACLE11GR2X64:1521/DTCUSR, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver}
30 12 2015 12:03:49,154  INFO Environment:317 - HHH000021: Bytecode provider name : javassist
Exception in thread "main" org.hibernate.boot.InvalidMappingException: Could not parse mapping document: Simulazione.hbm.xml (RESOURCE)
    at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:46)
    at org.hibernate.boot.jaxb.internal.UrlXmlSource.doBind(UrlXmlSource.java:36)
    at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:59)
    at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:499)
    at it.m2sc.simulator.Simulator.main(Simulator.java:10)
Caused by: java.lang.NullPointerException
    at com.sun.xml.bind.v2.model.impl.PropertyInfoImpl.calcXmlName(PropertyInfoImpl.java:287)
    at com.sun.xml.bind.v2.model.impl.PropertyInfoImpl.calcXmlName(PropertyInfoImpl.java:260)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl.getTypes(ElementPropertyInfoImpl.java:100)
    at com.sun.xml.bind.v2.model.impl.RuntimeElementPropertyInfoImpl.getTypes(RuntimeElementPropertyInfoImpl.java:50)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.size(ElementPropertyInfoImpl.java:42)
    at java.util.AbstractList$Itr.hasNext(AbstractList.java:339)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:139)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:49)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:41)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:189)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:204)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$3.run(JAXBContextImpl.java:352)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$3.run(JAXBContextImpl.java:350)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:349)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:215)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:76)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:363)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    at org.hibernate.boot.jaxb.internal.MappingBinder.hbmJaxbContext(MappingBinder.java:81)
    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:57)
    at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:43)
    ... 5 more

源代码:

package it.m2sc.simulator;

import it.m2sc.simulator.beans.Simulazione;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Simulator {
    public static void main(String[] args) {
        Configuration cfg = new Configuration();
        cfg.addResource("Simulazione.hbm.xml"); // CRASH HERE !!!
        SessionFactory factory = cfg.buildSessionFactory();
        Simulazione simulazione = factory.openSession().get(Simulazione.class, 41);
        System.out.println(simulazione);
    }
}

Simulazione.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="it.m2sc.simulator.beans">
    <class name="Simulazione" table="SIMULAZIONI">
        <id name="id" type="int" column="SIM_ID">
            <generator class="assigned" /> 
        </id>
        <property name="name" column="SIM_NAME" type="string" />
        <property name="description" column="SIM_DESCRIPTION" type="string" />
        <property name="type" column="SIM_TYPE" type="string" />
        <property name="envelopeCount" column="SIM_ENVELOPE_COUNT" type="int" />
        <property name="docCount" column="SIM_DOC_COUNT" type="int" />
        <property name="sheetCount" column="SIM_SHEET_COUNT" type="int" />
        <property name="percAccorp" column="SIM_PERC_ACCORP" type="int" />
        <property name="date" column="SIM_DATE" type="date" />
        <property name="status" column="SIM_STATUS" type="int" />
    </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:1)

您正在使用旧版本的JAXB。 版本2.2.11进行null检查:PropertyInfoImpl.java#371

case QUALIFIED:
    QName typeName = parent.getTypeName();
    if(typeName!=null) {
        uri = typeName.getNamespaceURI();
    } else {
        uri = xs.namespace();
    }
    if(uri.length()==0) {
        uri = parent.builder.defaultNsUri;
    }
    break;

版本2.0没有null支票:PropertyInfoImpl.java#286

case QUALIFIED:
    uri = parent.getTypeName().getNamespaceURI();
    if(uri.length()==0) {
        uri = parent.builder.defaultNsUri;
    }
    break;