These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: help with composite key + generator class for id
PostPosted: Mon Sep 26, 2005 2:24 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 10:26 am
Posts: 22
I seem to be stuck at a point and dont know how to go about , would appreicate some help. I have read the forums but i could not find something to help me out

INDIVIDUAL
|
*
INDIV_APP *-----APPLICATION
|
*
ANSWERS


I have four tables A,B,C,D .
Table (INDIVIDUAL)
indivId(PK)

Table (APPLICATION)
appId(PK)

Table (INDIV_APP ) Link table
indivId(FK)
appId(FK)

Table ( ANSWERS )
answerId (generated by sequence)
indivId(FK) from link table
appId(FK) from link table


How do i generate the id part for table ANSWERS ? which consist of an id + composite keys ?

when i generate the way i have it it . I get a mpping exception telling me that i need to have a foreign key of appId and indivId , for the ANSWER
table


which i do agree. But how does one use the id generator and composite id
to co exist or should i be doing something totally different ?

Code:
   
   <id name="id" column="ANSWER_ID" type="long">
              <generator class="sequence">
                <param name="sequence">ANS_SEQ</param>
            </generator>
       </id>


  &&
<composite-id name="id" class="XYZId" unsaved-value="any">
            <key-property name="individualId"   access="field"  column="INDIV_ID"       length="10"/>
            <key-property name="applicationId"  access="field"  column="APPLICATION_ID" length="10"/>       
        </composite-id>

   

to coexist ???



Hibernate version:3.05

Mapping documents:

Code:

[B] this is for table C[/B]
[code]
<hibernate-mapping package="common.orm.beans">

    <class name="IndivAppXtable" table="INDIV_APPS">
        <composite-id name="id" class="IndivAppXtableId1" unsaved-value="any">
            <key-property name="individualId"   access="field"  column="INDIV_ID"       length="10"/>
            <key-property name="applicationId"  access="field"  column="APPLICATION_ID" length="10"/>       
        </composite-id>
       
       
        <property name="artifactPrintDate" column="PRINT_DATE" type="date"  not-null="true" />
     
        <component name="rowHistory" class="RowHistory">
            <property name="createdBy"       column="CREATED_BY"    type="java.lang.String" length="20" not-null="true" />
            <property name="updatedBy"       column="UPDATED_BY"    type="java.lang.String" length="20" not-null="true" />
            <property name="dateCreated"     column="CREATION_DATE" type="java.util.Date"               not-null="true" />
            <property name="dateUpdated"     column="UPDATE_DATE"   type="java.util.Date"               not-null="true" />
        </component>
       
       
       <many-to-one name="individual"     column="INDIV_ID"              class="Individual"      insert="false" update="false" not-null="true"/>
       <many-to-one name="application"    column="APPLICATION_ID"         class="Application"     insert="false" update="false" not-null="true"/>
       
       
       
       <set name="answers" inverse="true" cascade="save-update">
       <key>
        <column name="individualId"></column>
        <column name="applicationId"></column>
       </key>
            <one-to-many class="Answer" />
        </set>
       
   
      <!--
      <set name="indivs" inverse="true" cascade="all">
         <key column="INDIV_ID"/>
         <one-to-many class="Answer"/>
      </set>
     
 
       <set name="apps" inverse="true" cascade="save-update">
            <key column="APPLICATION_ID"/>
            <one-to-many class="Answer" />
        </set>
       -->
       
       <!--
   
        -->
       
       
       
    </class>
</hibernate-mapping>
[/code]


[B] this is for table D [/B]
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >


<hibernate-mapping package="common.orm.beans">
    <class name="Answer" table="ANSWERS">
       

    <!--  this takes care of the pk for this table -->
      <id name="id" column="ANSWER_ID" type="long">
              <generator class="sequence">
                <param name="sequence">ANS_SEQ</param>
            </generator>
       </id>
       
       
       
       
       <property name="answerFlg"      column="ANSWER_FLG"      type="string"  not-null="true" />
       <property name="answerDesc"     column="ANSWER_DESC"     type="string" />
       
       <!-- component -->
       <component name="rowHistory" class="RowHistory">
            <property name="createdBy"       column="CREATED_BY"      type="java.lang.String" length="20" not-null="true" />
            <property name="updatedBy"       column="UPDATED_BY"      type="java.lang.String" length="20" not-null="true" />
            <property name="dateCreated"     column="CREATION_DATE"   type="java.util.Date"               not-null="true" />
            <property name="dateUpdated"     column="UPDATE_DATE"     type="java.util.Date"               not-null="true" />
       </component>



        <many-to-one name="question"         column="QUESTION_ID"     class="Question"           not-null="true"  insert="false" update="false"/>
        <many-to-one name="application"      column="APPLICATION_ID"  class="IndivAppXtable"     not-null="true"  insert="false" update="false"/>
        <many-to-one name="individual"       column="INDIV_ID"        class="IndivAppXtable"     not-null="true"  insert="false" update="false"/>

       
    </class>
   
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():


Full stack trace of any exception that occurs:
[code]
Foreign key (FK4FD94C2D61A73A30:ANSWERS [INDIV_ID])) must have same number of columns as the referenced primary key (INDIV_APPS [INDIV_ID,APPLICATION_ID])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:86)
at org.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:51)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:976)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:921)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
at util.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:49)
at driver.Driver.main(Driver.java:41)
java.lang.NullPointerException
at util.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:56)
at driver.Driver.main(Driver.java:41)
[code]



Name and version of the database you are using:
Oracle 9.2.0.1.0


The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:[quote][/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 2:33 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Quote:
which i do agree. But how does one use the id generator and composite id
to co exist or should i be doing something totally different ?


I'd go totally different. just make id your primary key by itself, and the other two individualID and applicaitonID foreign keys.

if you're using a sequence generator to generate a unique number every time a new row is inserted into that table, there is no reason to also have a composite id with values.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 2:44 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Here is a FAQ on this topic: http://www.hibernate.org/117.html#A35

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 1:50 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 10:26 am
Posts: 22
thankx nathanmoon and kochcp for your suggestion .
I opted for option 1 which i thought about but had not idea how to go about setting a one to many realtion relation ship with multiple foreign keys coming from a table.


I preferred not to use option 2 as it seemed to be a whole lot more work
I wanted to follow the first approach of having my intersect table (AppAnswer) generates it own id and have the foreign keys coming in from another intersect table (indivapp), but i had not idea how to do it with multiple foreign key


I was able to solve the problem

If any one is interested this is the way i was able to over come by problem

problem

Table (Individual)
|
*
Table intersect 1(IndivApp) *------ Table(Application)
|
*
Table intersect 2(ApplAnswer)

Heres the solution i used
in the

indivApp Mapping (intersect table 1)
-------------------
blah...blah blah
<!-- needed for the multiple foregin keys going into ApplAnswer Table-->
<set name="answers" inverse="true" cascade="save-update">
<key>
<column name="INDIV_ID"></column>
<column name="APPLICATION_ID"></column>

</key>
<one-to-many class="Answer" />
</set>



ApplAnswer Mapping (intersect table 2)
------------------------

<many-to-one name="indivApp" class="IndivApp" not-null="true">
<column name="INDIV_ID"></column>
<column name="APPLICATION_ID"></column>
</many-to-one>


I found that the traditional approach of having
insert= false and update=false where not allowing it to insert the foreign keys. So i took them out and it worked


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 4:07 am 
Newbie

Joined: Sun Oct 15, 2006 10:50 pm
Posts: 2
rascaly2k wrote:

I was able to solve the problem

If any one is interested this is the way i was able to over come by problem

problem

Table (Individual)
|
*
Table intersect 1(IndivApp) *------ Table(Application)
|
*
Table intersect 2(ApplAnswer)

Heres the solution i used
in the

indivApp Mapping (intersect table 1)
-------------------
blah...blah blah
<!-- needed for the multiple foregin keys going into ApplAnswer Table-->
<set name="answers" inverse="true" cascade="save-update">
<key>
<column name="INDIV_ID"></column>
<column name="APPLICATION_ID"></column>

</key>
<one-to-many class="Answer" />
</set>



ApplAnswer Mapping (intersect table 2)
------------------------

<many-to-one name="indivApp" class="IndivApp" not-null="true">
<column name="INDIV_ID"></column>
<column name="APPLICATION_ID"></column>
</many-to-one>


I found that the traditional approach of having
insert= false and update=false where not allowing it to insert the foreign keys. So i took them out and it worked


Hi All,
my problem is similar to the above, However the solution posted by
rascaly2k does not solve the problem i still keep getting the same
error

Foreign key (FK37C60C471A8CA8EF:TRAXON_CATEGORY_DETAIL [CATEGORY_ID,SUBCAT_ID])) must have same number of columns as the referenced primary key (TRAXON_SUBCATEGORY [SUBCAT_ID])

i have carefully double checked the mappings, and it is same as above
am i missing something?

appreciate if some one could let me know do i need any other mappings/settings

thanks a ton


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.