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.  [ 4 posts ] 
Author Message
 Post subject: sequence generator and foreign key to it from one-to-many
PostPosted: Tue Aug 25, 2009 6:38 am 
Newbie

Joined: Tue Aug 25, 2009 5:56 am
Posts: 4
Hi all
I'm faceing a problem and i seek for your help

I have 3 tables :
tblEvent - PK pk_iEventNumber
tblEventDescription - PK pkfk_iEventNumber
tblEventFields - composed PK (pkfk_iEventNumber, pk_iFieldIndex)

between tblEvent and tblEventDescription there is a relation one-to-one ( one way- event has a description(always), no need other direction)
between tblEventDescription and tblEventFields there is a one-to-many relation ( one event description has none or multiple fields)

Event mapping
Code:
<hibernate-mapping>
     <class name="mypackage.Event" table="tblEvent">

        <id name="EventNo">
            <column name="pk_iEventNo" sql-type="integer"/>
            <generator class="native"/>
        </id>

        <property ...
         .................
        </property>

        <one-to-one cascade="all" class="mypackage.EventDescription" name="event2EventDescriptionRef"/>

    </class>
</hibernate-mapping>



Event description mapping
Code:
<hibernate-mapping>
     <class name="mypackage.EventDescription" table="tblEventDescription">
         <id name="EventNo">
            <column name="pkfk_iEventNo" sql-type="integer"/>
            <generator class="foreign">
                <param name="property">eventDescription2EventRef</param>
            </generator>
        </id>

        <property ...
         .................
        </property>

        <one-to-one cascade="all" class="mypackage.Event" name="eventDescription2EventRef" constrained="true"/>

         <set cascade="all" inverse="true" name="eventDescription2EventFieldsRef">
                 <key>
                    <column name="pkfk_iEventNo" not-null="true"/>
                 </key>
            <one-to-many class="mypackage.EventFields"/>
        </set>

    </class>
</hibernate-mapping>


Event fields mapping
Code:
<hibernate-mapping>
     <class name="mypackage.EventFields" table="tblEventFields">
     
        <composite-id class="mypackage.EventFieldsPK" mapped="true">
            <key-property name="EventNo">
                <column name="pkfk_iEventNo" sql-type="integer"/>
            </key-property>
            <key-property name="fieldIndex">
                <column name="pk_iFieldIndex" not-null="true" sql-type="integer"/>
            </key-property>
        </composite-id>

        <property name="fieldValue">
            <column name="szFieldValue" not-null="false" sql-type="varchar(300)"/>
        </property>

     </class>
</hibernate-mapping>



This is my current configuration.
As it can be see i want that eventNo to be generated sequencial by hibernate.
If i insert in db a event with description but no event fields attached operations goes fine; but if i want to add some fields then i receive error column 'pkfk_iEventNo' in table 'tblEbentFields' cannot be NULL which is very normal, since indeed eventNo is null, and i don't indicate hibernate that it should be generated as "foreign" from paretnEventNo.

So my first question is how to solve that, to be able to insert in db a full event( with description, and fields)
My second question is that is realy necesary to use other-way 1to1 reference eventDescription2EventRef (from eventDescription to event). i added just to be able to declare eventNo from EventDescription as a foreign generator

<column name="pkfk_iEventNo" sql-type="integer"/>
<generator class="foreign">
<param name="property">eventDescription2EventRef</param>
</generator>

There is another way to do that ?

Thank you very much for your time and sorry if i didn'd know how to describe this issue better.
Best regards
grandanat


Top
 Profile  
 
 Post subject: Re: sequence generator and foreign key to it from one-to-many
PostPosted: Tue Aug 25, 2009 11:21 am 
Newbie

Joined: Tue Aug 25, 2009 5:56 am
Posts: 4
Above i described my specific problem, which can be generalized. So the main idea is that i have 2 tables:

tableParent with primary key simple parentID
tableChild with primary key composed (parentID, childID)

and between them there is a one-to-many relation, from parent to child, then:

if i want that parentID to be generated automatically by hibernate, i declare a generator (native, increment, sequence, etc);
but how to announce hibernate that parentID from tableChild should (pre)get the generated value for parent table, thereby error null field to not be thrown anymore.


Top
 Profile  
 
 Post subject: Re: sequence generator and foreign key to it from one-to-many
PostPosted: Tue Sep 01, 2009 4:02 am 
Newbie

Joined: Tue Aug 25, 2009 5:56 am
Posts: 4
Did nobody faced this problem before ? or similar one ?


Top
 Profile  
 
 Post subject: Re: sequence generator and foreign key to it from one-to-many
PostPosted: Wed Oct 12, 2011 2:24 am 
Newbie

Joined: Fri Dec 03, 2010 1:03 pm
Posts: 4
I have the exact same problem...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.