-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite-id and insert/updates
PostPosted: Tue Jun 15, 2004 11:33 am 
Newbie

Joined: Tue Jun 15, 2004 11:11 am
Posts: 1
I have the following mappings:

Code:
<hibernate-mapping>
<class name="some.package.ApplicationEvidence" table="application_evidence" >

    <composite-id name="pk" class="some.package.ApplicationEvidencePK">
      <key-many-to-one name="applicationValidation" class="some.package.ApplicationValidation">
           <column name="ava_id" />
      </key-many-to-one>
      <key-many-to-one name="customerEvidence" class="some.package.CustomerEvidence">
           <column name="cev_id" />
      </key-many-to-one>
    </composite-id>   

    <!-- Some properties -->

</class>


<class name="some.package.ApplicationValidation" table="application_validations">


    <id name="id" type="java.lang.Long" column="id">
        <generator class="sequence">
                <param name="sequence">ava_seq</param>
        </generator>
    </id>

    <!-- other properties ... -->

    <set name="applicationEvidence" lazy="true" inverse="true" cascade="all">
        <key>
            <column name="ava_id" />
        </key>
        <one-to-many class="some.package.ApplicationEvidence" />
    </set>
</class>

<class name="some.package.CustomerEvidence" table="customer_evidence">

    <id name="id" type="java.lang.Long" column="id">
        <generator class="sequence">
                <param name="sequence">cev_seq</param>
        </generator>
    </id>

    <set name="applicationEvidence" lazy="true" inverse="true" cascade="all">
        <key>
            <column name="cev_id" />
        </key>
        <one-to-many
            class="some.package.ApplicationEvidence"
        />
    </set>

</class>
</hibernate-mapping>



Table ApplicationEvidence has a composite primary key involving two columns, both of which are foreign keys to ApplicationValidation and CustomerEvidence respectively.

In my java code I create both a CustomerEvidence and ApplicationValidation object, and then create the ApplicationEvidencePK using the two objects (without first saving anything).

i.e.
Code:
CustomerEvidence custEv = new CustomerEvidence(...., new HashSet(),...);
ApplicationValidation appVal = new ApplicationValidation(....,new HashSet()...);
ApplicationEvidencePK pk = new ApplicationEvidencePK(custEv,appVal);
ApplicationEvidence evid = new ApplicationEvidence(pk);
custEv.getApplicationEvidence().add(evid);
appVal.getApplicationEvidence().add(evid);

....
...
//save the application


When I save, the CustomerEvidence and ApplicationValidation objects are stored correctly but the ApplicationEvidence object is not inserted and I get an update statement on my console output instead of an insert.

Incidentally, while mucking about trying to get this to work, i added the unsaved-value="any" to my composite-id tag, and I then recieved an integrity constraint error, hibernate was finally trying to insert the ApplicationEvidence object, but did so before the CustomerEvidence object.

Can anyone shed some light on this?


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

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.