Hibernate映射错误

时间:2009-09-16 10:18:52

标签: java hibernate

我创建了一个.hbm.xml文件:

<xml version="1.0"?>


<hibernate-mapping package="com.cmp.iard.sinistre.hbm.dossier">
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd >


    <class
        name="Personne"
        table="NOYS_PERSONNE"
    >
        <meta attribute="sync-DAO">false</meta>

        <id
            name="id"
            type="string"
            column="CODE_PERS"
        >
            <generator class="assigned" />
        </id>

        <!--  
        <property
            name="codePers"
            column="CODE_PERS"
            type="string"
            not-null="true"
            length="25"
        />
        -->

        <many-to-one
            name="codeObjParamSinistre"
            column="CODE_OBJ_PARAM_SINISTRE"
            class="com.afrikbrain.iard.sinistre.hbm.ref.Paramsinistre"
            not-null="false"
        >
        </many-to-one>

        <!--  
        <property
            name="codeObjParamSinistre"
            column="CODE_OBJ_PARAM_SINISTRE"
            type="string"
            not-null="true"
            length="25"
        />
        -->

        <many-to-one
            name="codeMissionExpert"
            column="CODE_MISSION_EXPERT"
            class="MissionExpert"
            not-null="false"
        >
        </many-to-one>

        <!--  
        <property
            name="codeMissionExpert"
            column="CODE_MISSION_EXPERT"
            type="string"
            not-null="true"
            length="25"
        />
        -->

        <many-to-one
            name="codeProfession"
            column="CODE_PROF"
            class="com.cmp.iard.srv.ref.Profession"
            not-null="false"
        >
        </many-to-one>


        <many-to-one
            name="codeSousDossier"
            column="CODE_SOUS_DOSSIER"
            class="SousDossier"
            not-null="false"
        >
        </many-to-one>

        <property
            name="nom"
            column="NOM_PERS"
            type="string"
            not-null="false"
            length="100"            
        />
        <property
            name="localisation"
            column="LOCALISATION_PERS"
            type="string"
            not-null="false"
            length="100"
        />
        <property
            name="telephone"
            column="TELEPHONE_PERS"
            type="string"
            not-null="false"
            length="100"
        />
        <property
            name="adresse"
            column="ADRESSE_PERS"
            type="string"
            not-null="false"
            length="100"
        />

        <!--  
        <property
            name="type"
            column="TYPE_PERS"
            type="string"
            not-null="false"
            length="5"
        />
        -->


        <property
            name="titre"
            column="TITRE_PERS"
            type="string"
            not-null="false"
            length="25"
        />


        <property
            name="dateNaissance"
            column="DATE_NAISS_PERS"
            type="date"
            not-null="false"
            length="7"
        />
        <property
            name="lieuNaissance"
            column="LIEU_NAISS_PERS"
            type="string"
            not-null="false"
            length="50"
        />
        <property
            name="numeroPermis"
            column="NUM_PERMIS_PERS"
            type="string"
            not-null="false"
            length="50"
        />
        <property
            name="categoriePermis"
            column="CATEG_PERMIS_PERS"
            type="string"
            not-null="false"
            length="50"
        />
        <property
            name="numeroID"
            column="NUM_ID_PERS"
            type="string"
            not-null="false"
            length="50"
        />
        <property
            name="dateID"
            column="DATE_ID_PERS"
            type="date"
            not-null="false"
            length="7"
        />
        <property
            name="lieuID"
            column="LIEU_ID_PERS"
            type="string"
            not-null="false"
            length="50"
        />
        <property
            name="codeUti"
            column="CODE_UTI"
            type="string"
            not-null="false"
            length="25"
        />
        <property
            name="dteUti"
            column="DTE_UTI"
            type="date"
            not-null="false"
            length="7"
        />
        <property
            name="codeUtiModif"
            column="CODE_UTI_MODIF"
            type="string"
            not-null="false"
            length="25"
        />
        <property
            name="dteUtiModif"
            column="DTE_UTI_MODIF"
            type="date"
            not-null="false"
            length="7"
        />

        <discriminator column="TYPE_PERS" type="string" />

        <subclass name="Declarant" discriminator-value="DE">
        </subclass>

        <subclass name="Beneficiaire" discriminator-value="BE">

            <many-to-one
            name="idTransaction"
            column="ID_TRANSACTION"
            class="com.cmp.iard.sinistre.hbm.evaluation.Transaction"
            not-null="false"
            >
            </many-to-one>

            <many-to-one
            name="codeTypeBeneficiaire"
            column="CODE_TYPE_BENEFICIAIRE"
            class="com.cmp.iard.sinistre.hbm.ref.TypeBeneficiaire"
            not-null="false"
            >
            </many-to-one>          

            <property
            name="estTransige"
            column="EST_TRANSIGE"
            type="string"
            />

        </subclass>

        <subclass name="Adversaire" discriminator-value="AD">

            <!--  
            <property
            name="codePers"
            column="CODE_PERS"
            type="string"
            not-null="true"
            length="25"
            />


            <property
            name="codeSousDossier"
            column="CODE_SOUS_DOSSIER"
            type="string"
            not-null="true"
            length="25"
            />
            -->
        </subclass>

        <subclass name="Temoin" discriminator-value="TE">

            <!--
            <property
            name="codePers"
            column="CODE_PERS"
            type="string"
            not-null="true"
            length="25"
            />


            <property
            name="codeSousDossier"
            column="CODE_SOUS_DOSSIER"
            type="string"
            not-null="true"
            length="25"
            />
            -->

        </subclass>             


    </class>    
</hibernate-mapping>

这是表格:

CREATE TABLE SYS.NOYS_PERSONNE
(
  CODE_PERS                VARCHAR2(25 BYTE)    NOT NULL,
  CODE_OBJ_PARAM_SINISTRE  VARCHAR2(25 BYTE),
  CODE_MISSION_EXPERT      VARCHAR2(25 BYTE),
  CODE_PROF                VARCHAR2(25 BYTE),
  NOM_PERS                 VARCHAR2(100 BYTE),
  LOCALISATION_PERS        VARCHAR2(100 BYTE),
  TELEPHONE_PERS           VARCHAR2(100 BYTE),
  ADRESSE_PERS             VARCHAR2(100 BYTE),
  TYPE_PERS                VARCHAR2(5 BYTE),
  TITRE_PERS               VARCHAR2(25 BYTE),
  DATE_NAISS_PERS          DATE,
  LIEU_NAISS_PERS          VARCHAR2(50 BYTE),
  NUM_PERMIS_PERS          VARCHAR2(50 BYTE),
  CATEG_PERMIS_PERS        VARCHAR2(50 BYTE),
  NUM_ID_PERS              VARCHAR2(50 BYTE),
  DATE_ID_PERS             DATE,
  LIEU_ID_PERS             VARCHAR2(50 BYTE),
  CODE_UTI                 VARCHAR2(25 BYTE),
  DTE_UTI                  DATE,
  CODE_UTI_MODIF           VARCHAR2(25 BYTE),
  DTE_UTI_MODIF            DATE,
  ID_TRANSACTION           VARCHAR2(25 BYTE),
  CODE_SOUS_DOSSIER        VARCHAR2(25 BYTE),
  CODE_TYPE_BENEFICIAIRE   VARCHAR2(25 BYTE),
  EST_TRANSIGE             VARCHAR2(1 BYTE)
)

以下是适用于该表的约束:

- 
-- Foreign Key Constraints for Table NOYS_PERSONNE 
-- 
ALTER TABLE SYS.NOYS_PERSONNE ADD (
  CONSTRAINT FK_NOYS_PER_ASS29_NOYS_MIS 
 FOREIGN KEY (CODE_MISSION_EXPERT) 
 REFERENCES SYS.NOYS_MISSION_EXPERT (CODE_MISSION_EXPERT));

ALTER TABLE SYS.NOYS_PERSONNE ADD (
  CONSTRAINT FK_NOYS_PER_TYPEPIECE_NOYPS_PA 
 FOREIGN KEY (CODE_OBJ_PARAM_SINISTRE) 
 REFERENCES SYS.NOYPS_PARAMSINISTRE (CODE_OBJ_PARAM_SINISTRE));

ALTER TABLE SYS.NOYS_PERSONNE ADD (
  CONSTRAINT NOYS_PERSONNE_R03 
 FOREIGN KEY (CODE_PROF) 
 REFERENCES OMEFIRE.NOYP_PROFESSION (CODE_PROF));

ALTER TABLE SYS.NOYS_PERSONNE ADD (
  FOREIGN KEY (CODE_SOUS_DOSSIER) 
 REFERENCES SYS.NOYS_SOUS_DOSSIER (CODE_SOUS_DOSSIER));

ALTER TABLE SYS.NOYS_PERSONNE ADD (
  FOREIGN KEY (ID_TRANSACTION) 
 REFERENCES SYS.NOYS_TRANSACTION (ID_TRANSACTION));




ALTER TABLE SYS.NOYS_ADVERSAIRE ADD (
  CONSTRAINT FK_NOYS_ADV_GENERALIS_NOYS_PER 
 FOREIGN KEY (CODE_PERS) 
 REFERENCES SYS.NOYS_PERSONNE (CODE_PERS));

ALTER TABLE SYS.NOYS_SINISTRE_BASE ADD (
  CONSTRAINT FK_NOYS_SIN_DECLARANT_NOYS_PER 
 FOREIGN KEY (CODE_PERS) 
 REFERENCES SYS.NOYS_PERSONNE (CODE_PERS));

ALTER TABLE SYS.NOYS_TEMOIN ADD (
  CONSTRAINT FK_NOYS_TEM_GENERALIS_NOYS_PER 
 FOREIGN KEY (CODE_PERS) 
 REFERENCES SYS.NOYS_PERSONNE (CODE_PERS));

ALTER TABLE SYS.NOYS_DETAIL_DECOMPTE ADD (
  FOREIGN KEY (CODE_PERS) 
 REFERENCES SYS.NOYS_PERSONNE (CODE_PERS));

每当我尝试从Eclipse运行它时,我都会收到以下错误:

11:09:01,718 INFO  [Configuration] Searching for mapping documents in jar: AfrikBrain.AssurHibernate.har
11:09:01,718 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysAssDeclPtEval.hbm.xml
11:09:01,750 INFO  [HbmBinder] Mapping class: com.afrikbrain.iard.sinistre.hbm.dossier.PointsEvaluationDeclaration -> NOYS_ASS_DECL_PT_EVAL
11:09:01,765 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysCausesAnnexesSinistre.hbm.xml
11:09:01,781 INFO  [HbmBinder] Mapping class: com.afrikbrain.iard.sinistre.hbm.dossier.CausesAnnexesSinistre -> NOYS_CAUSESANNEXES_SINISTRE
11:09:01,781 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysDeclarationAuto.hbm.xml
11:09:01,796 INFO  [HbmBinder] Mapping class: com.afrikbrain.iard.sinistre.hbm.dossier.DeclarationAuto -> NOYS_DECLARATION_AUTO
11:09:01,796 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysDerogation.hbm.xml
11:09:01,796 INFO  [HbmBinder] Mapping class: com.afrikbrain.iard.sinistre.hbm.dossier.Derogation -> NOYS_DEROGATION
11:09:01,812 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysMissionExpert.hbm.xml
11:09:01,812 INFO  [HbmBinder] Mapping class: com.afrikbrain.iard.sinistre.hbm.dossier.MissionExpert -> NOYS_MISSION_EXPERT
11:09:01,812 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysMotifdero.hbm.xml
11:09:01,828 INFO  [HbmBinder] Mapping class: com.afrikbrain.iard.sinistre.hbm.dossier.MotifDerogation -> NOYS_MOTIFDERO
11:09:01,828 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysMotifrefus.hbm.xml
11:09:01,843 INFO  [HbmBinder] Mapping class: com.afrikbrain.iard.sinistre.hbm.dossier.Motifrefus -> NOYS_MOTIFREFUS
11:09:01,843 INFO  [Configuration] Found mapping document in jar: com/afrikbrain/iard/sinistre/hbm/dossier/NoysPersonne.hbm.xml
11:09:01,843 ERROR [XMLHelper] Error parsing XML: XML InputStream(288) The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".
11:09:01,859 WARN  [ServiceController] Problem starting service AssurHibernate.har:service=Hibernate
org.hibernate.InvalidMappingException: Could not read mapping documents from jar: AfrikBrain.AssurHibernate.har
    at org.hibernate.cfg.Configuration.addJar(Configuration.java:621)
    at org.jboss.hibernate.jmx.Hibernate.handleMappings(Hibernate.java:318)
    at org.jboss.hibernate.jmx.Hibernate.buildConfiguration(Hibernate.java:208)
    at org.jboss.hibernate.jmx.Hibernate.buildSessionFactory(Hibernate.java:228)
    at org.jboss.hibernate.jmx.Hibernate.startService(Hibernate.java:155)
    at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
    at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
    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 org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
    at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
    at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
    at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
    at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
    at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
    at $Proxy0.start(Unknown Source)
    at org.jboss.system.ServiceController.start(ServiceController.java:417)
    at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
    at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
    at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
    at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
    at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
    at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
    at $Proxy4.start(Unknown Source)
    at org.jboss.deployment.SARDeployer.start(SARDeployer.java:302)
    at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1025)
    at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:819)
    at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:782)
    at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
    at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
    at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
    at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
    at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
    at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
    at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
    at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
    at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
    at $Proxy10.deploy(Unknown Source)
    at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:421)
    at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:634)
    at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:263)
    at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.loop(AbstractDeploymentScanner.java:274)
    at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.run(AbstractDeploymentScanner.java:225)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:502)
    at org.hibernate.cfg.Configuration.addJar(Configuration.java:618)
    ... 50 more
Caused by: org.xml.sax.SAXParseException: The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(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.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
    ... 51 more
11:09:01,875 ERROR [URLDeploymentScanner] Incomplete Deployment listing:

--- MBeans waiting for other MBeans ---
ObjectName: AssurHibernate.har:service=Hibernate
  State: FAILED
  Reason: org.hibernate.InvalidMappingException: Could not read mapping documents from jar: AfrikBrain.AssurHibernate.har

--- MBEANS THAT ARE THE ROOT CAUSE OF THE PROBLEM ---
ObjectName: AssurHibernate.har:service=Hibernate
  State: FAILED
  Reason: org.hibernate.InvalidMappingException: Could not read mapping documents from jar: AfrikBrain.AssurHibernate.har

我的.hbm.xml文件出了什么问题?

2 个答案:

答案 0 :(得分:13)

首先,XML文件的格式全部搞砸了,需要重写为:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.cmp.iard.sinistre.hbm.dossier">

其次,根据DTD,映射的顺序无效。您需要移动<discriminator/>元素,使其位于<id/>元素之后,而不是靠近<subclass/>元素:

<class name="Personne" table="NOYS_PERSONNE">
    <meta attribute="sync-DAO">false</meta>

    <id name="id" type="string" column="CODE_PERS">
       <generator class="assigned" />
    </id>
    <discriminator column="TYPE_PERS" type="string" />

    .... rest of mapping goes here ....
</class>

答案 1 :(得分:2)

我认为根据以下声明,你应该在属性和包元素之前的id元素之后放置discriminator元素

  

元素类型“class”的内容   必须匹配   “(元*,子查询,缓存,同步*,评论,*的tuplizer(ID?|?复合-ID),鉴别,自然-ID,(版本?|?时间戳),(财产|?许多-TO-一个|一到一个|分量|动态分量|特性|任何|地图|设为|列表|袋| idbag |阵列|原始阵列)的,((合并,子类*)|接合子类* |工会子类*),装载机,SQL插入,SQL更新,SQL-删除,滤波器*,结果集*,(查询|????。SQL查询)*)”

如果不是这种情况,我建议您使用hibernate-mapping架构验证您的xml。