facebook

Exception caused by hibernate mapping

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #284650 Reply

    saif
    Member

    Hi every body,
    I work with MyEclipse 6 and i used it to map may database.
    when i tried an hibernate example i got the following exception :

    
    Caused by: org.hibernate.MappingException: Foreign key (FKB8DCB6CF723F2E2:idconv [GBAGCDAG])) must have same number of columns as the referenced primary key (gb2agen [GBAGCDAG,GBAGSOAG,GBAGEFPA,GBAGAFFS,GBAGCDAL,GBAGACSA,GBAGNUPR,GBAGDTPR,GBAGSOES,NUMIDE,GBAGDTIN,GBAGDTFI,GBAGCDRA])
        at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
        at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
        at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1145)
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1052)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)
        at com.cpa.spi.dao.persona.HibernateUtil.<clinit>(HibernateUtil.java:20)
        ... 4 more
    

    this is the SQL of the tow tables concerned in the exception :

    
    CREATE TABLE IDCONV ( 
        COD_CON INTEGER NOT NULL , 
        GBAGCDTA CHAR(6) DEFAULT NULL , 
        GBAGCDAG CHAR(6) DEFAULT NULL , 
        GBSRCDSR CHAR(4) DEFAULT NULL , 
        PESECDSE CHAR(4) DEFAULT NULL , 
        COD_MOT_CON INTEGER DEFAULT NULL , 
        COD_MIT_CON INTEGER DEFAULT NULL , 
        COD_LLO_CON INTEGER DEFAULT NULL , 
        COD_PER INTEGER DEFAULT NULL , 
        DAT_CON DATE DEFAULT NULL , 
        HOR_CON TIME DEFAULT NULL , 
        DAT_TAN_CON DATE DEFAULT NULL , 
        HOR_TAN_CON TIME DEFAULT NULL , 
        CONSTRAINT PK_IDCONV PRIMARY KEY( COD_CON ) ) ; 
      
    ALTER TABLE IDCONV 
        ADD CONSTRAINT FK_GB2AGEN_IDCONV 
        FOREIGN KEY( GBAGCDAG ) 
        REFERENCES GB2AGEN ( GBAGCDAG ) 
        ON DELETE NO ACTION 
        ON UPDATE NO ACTION ; 
      
    ALTER TABLE IDCONV 
        ADD CONSTRAINT FK_GB2AGEN_IDCONV_TA 
        FOREIGN KEY( GBAGCDTA ) 
        REFERENCES GB2AGEN ( GBAGCDAG ) 
        ON DELETE NO ACTION 
        ON UPDATE NO ACTION ; 
      
    ALTER TABLE IDCONV 
        ADD CONSTRAINT FK_IDMTCV_IDCONV 
        FOREIGN KEY( COD_MOT_CON ) 
        REFERENCES IDMTCV ( COD_MOT_CON ) 
        ON DELETE NO ACTION 
        ON UPDATE NO ACTION ; 
      
    ALTER TABLE IDCONV 
        ADD CONSTRAINT FK_PE2SECC_IDCONV 
        FOREIGN KEY( GBSRCDSR , PESECDSE ) 
        REFERENCES PE2SECC ( GBSRCDSR , PESCCDSC ) 
        ON DELETE NO ACTION 
        ON UPDATE NO ACTION ; 
      
    ALTER TABLE IDCONV 
        ADD CONSTRAINT FK_IDPERS_IDCONV 
        FOREIGN KEY( COD_PER ) 
        REFERENCES IDPERS ( COD_PER ) 
        ON DELETE NO ACTION 
        ON UPDATE NO ACTION ; 
      
    ALTER TABLE IDCONV 
        ADD CONSTRAINT FK_IDLLCV_IDCONV 
        FOREIGN KEY( COD_LLO_CON ) 
        REFERENCES IDLLCV ( COD_LLO_CON ) 
        ON DELETE NO ACTION 
        ON UPDATE NO ACTION ; 
      
    ALTER TABLE IDCONV 
        ADD CONSTRAINT FK_IDMICV_IDCONV 
        FOREIGN KEY( COD_MIT_CON ) 
        REFERENCES IDMICV ( COD_MIT_CON ) 
        ON DELETE NO ACTION 
        ON UPDATE NO ACTION ; 
    

    and

    
    CREATE TABLE GB2AGEN ( 
        GBAGCDAG CHAR(6) NOT NULL , 
        GBAGSOAG CHAR(35) NOT NULL , 
        GBAGEFPA DECIMAL(1, 0) DEFAULT 0 , 
        GBAGAFFS DECIMAL(1, 0) DEFAULT 0 , 
        GBAGCDAL CHAR(5) DEFAULT NULL , 
        GBAGACSA DECIMAL(1, 0) DEFAULT 1 , 
        GBAGNUPR DECIMAL(2, 0) DEFAULT NULL , 
        GBAGDTPR DATE DEFAULT NULL , 
        GBAGSOES DECIMAL(1, 0) NOT NULL DEFAULT 0 , 
        NUMIDE DECIMAL(6, 0) DEFAULT NULL , 
        GBAGDTIN DATE DEFAULT NULL , 
        GBAGDTFI DATE DEFAULT NULL , 
        GBAGCDRA CHAR(5) DEFAULT NULL , 
        CONSTRAINT QSYS_GB2AGEN_00001 PRIMARY KEY( GBAGCDAG ) ) ; 
    

    and this is a portion of code of the hbm file

    
    
    <hibernate-mapping>
        <class name="Convocatoria" table="idconv" catalog="i1bdd2">
            <id name="codCon" type="java.lang.Integer">
                <column name="COD_CON" />
                <generator class="assigned" />
            </id>
            <many-to-one name="pe2secc" class="SectorPolicia"
                fetch="select">
                <column name="GBSRCDSR" length="4" />
                <column name="PESECDSE" length="4" />
            </many-to-one>
            <many-to-one name="idpers" class="Persona" fetch="select">
                <column name="COD_PER" />
            </many-to-one>
            <many-to-one name="gb2agenByGbagcdag" class="AgentPolicia"
                fetch="select">
                <column name="GBAGCDAG" length="6" />
            </many-to-one>
            <many-to-one name="gb2agenByGbagcdta" class="AgentPolicia"
                fetch="select">
                <column name="GBAGCDTA" length="6" />
            </many-to-one>
            <many-to-one name="idmtcv" class="MotiuConvocatoria"
                fetch="select">
                <column name="COD_MOT_CON" />
            </many-to-one>
            <many-to-one name="idmicv" class="MitjaConvocatoria"
                fetch="select">
                <column name="COD_MIT_CON" />
            </many-to-one>
            <many-to-one name="idllcv" class="LlocConvocatoria"
                fetch="select">
                <column name="COD_LLO_CON" />
            </many-to-one>
    ...
    

    Thank you in advance for your help.

    #284652 Reply

    saif
    Member

    I forgot to put the hbm file of the gb2agen table :

    
    <hibernate-mapping>
        <class name="AgentPolicia" table="gb2agen" catalog="i1bdd2">
            <composite-id name="id" class="AgentPoliciaId">
                <key-property name="gbagcdag" type="java.lang.String">
                    <column name="GBAGCDAG" length="6" />
                </key-property>
                <key-property name="gbagsoag" type="java.lang.String">
                    <column name="GBAGSOAG" length="35" />
                </key-property>
                <key-property name="gbagefpa" type="java.lang.Long">
                    <column name="GBAGEFPA" precision="1" scale="0" />
                </key-property>
                <key-property name="gbagaffs" type="java.lang.Long">
                    <column name="GBAGAFFS" precision="1" scale="0" />
                </key-property>
                <key-property name="gbagcdal" type="java.lang.String">
                    <column name="GBAGCDAL" length="5" />
                </key-property>
                <key-property name="gbagacsa" type="java.lang.Long">
                    <column name="GBAGACSA" precision="1" scale="0" />
                </key-property>
                <key-property name="gbagnupr" type="java.lang.Long">
                    <column name="GBAGNUPR" precision="2" scale="0" />
                </key-property>
                <key-property name="gbagdtpr" type="java.util.Date">
                    <column name="GBAGDTPR" length="0" />
                </key-property>
                <key-property name="gbagsoes" type="java.lang.Long">
                    <column name="GBAGSOES" precision="1" scale="0" />
                </key-property>
                <key-property name="numide" type="java.lang.Long">
                    <column name="NUMIDE" precision="6" scale="0" />
                </key-property>
                <key-property name="gbagdtin" type="java.util.Date">
                    <column name="GBAGDTIN" length="0" />
                </key-property>
                <key-property name="gbagdtfi" type="java.util.Date">
                    <column name="GBAGDTFI" length="0" />
                </key-property>
                <key-property name="gbagcdra" type="java.lang.String">
                    <column name="GBAGCDRA" length="5" />
                </key-property>
            </composite-id>
            <set name="idconvsForGbagcdta" inverse="true">
                <key>
                    <column name="GBAGCDTA" length="6" />
                </key>
                <one-to-many class="Convocatoria" />
            </set>
            <set name="idtfpes" inverse="true">
                <key>
                    <column name="GBAGCDAG" length="6" />
                </key>
                <one-to-many class="TelefonPersona" />
            </set>
            <set name="iddopes" inverse="true">
                <key>
                    <column name="GBAGCDAG" length="6" />
                </key>
                <one-to-many class="DomiciliPersona" />
            </set>
            <set name="idconvsForGbagcdag" inverse="true">
                <key>
                    <column name="GBAGCDAG" length="6" />
                </key>
                <one-to-many class="Convocatoria" />
            </set>
            <set name="idtfdos" inverse="true">
                <key>
                    <column name="GBAGCDAG" length="6" />
                </key>
                <one-to-many class="Telefondomicili" />
            </set>
            <set name="idftprs" inverse="true">
                <key>
                    <column name="GBAGCDAG" length="6" not-null="true" />
                </key>
                <one-to-many class="FotoPersona" />
            </set>
            <set name="idfamis" inverse="true">
                <key>
                    <column name="GBAGCDAG" length="6" />
                </key>
                <one-to-many class="Familia" />
            </set>
        </class>
    </hibernate-mapping>
    
    #284653 Reply

    saif
    Member

    I start to understant th problem. MyEclipse generate to me an AgentPoliciaId class Id with the different attribute of the table. but the FK in the idconv table is referenced to one colum in gb2agen table wich’s name is GBAGCDAG.
    How can i resolve this probleme please ?

    #284682 Reply

    Riyad Kalla
    Member

    saif,
    The default behavior is to generate a composite id when:
    * No PK is set on the table
    * A composite PK is set on the table

    Are there any additional ALTER TABLE commands that go along with the gb2agen table? I don’t see any above, but I see a lot with the previous table, so I was wondering if this one has one as well.

    #284702 Reply

    saif
    Member

    Thank you support-rkalla for your response,
    i’m sur that there is no alter table in gb2agen.

    #284703 Reply

    saif
    Member

    reading your answer support-rKalla i had the idea of changing composit id by a simple one.and it really resolved the problem. No, i don’t have the exception above.
    But what i should do if i want to make the join with a composit id in this case?

    #284735 Reply

    Riyad Kalla
    Member

    saif,
    I’m glad to hear you resolved the issue. As for the second issue, I don’t quite follow… you shouldn’t need to perform any joins manually with related entities (o2m, m2o, m2m) because the entities will be generated with the appropriate getters and setters to access their related entities for you. For example a.getB().

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: Exception caused by hibernate mapping

You must be logged in to post in the forum log in