-->
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: Composite elements mapping question
PostPosted: Fri Jun 10, 2005 5:14 am 
Beginner
Beginner

Joined: Mon Apr 11, 2005 8:37 am
Posts: 22
Hi,
does a mapping like the following make sense?
In a class RequestAnswer I want a collection of composite elements (RequestAnswerElements). Those are stored in another table, a join is done one the primary key of RequestAnswer.
From the DTD perspective this kind of mapping is allowed. Hibernate loads it perfectly. Is there a proposed way to handle such objects in Java (any experience with such a mapping?) How does Hibernate knows to what parent object a composite element belongs? I guess I miss something in the picture, please bring some light into the dark...

Code:
<set name="requestAnswerElements" lazy="false" inverse="true" cascade="none" table="REQUEST_ANS_ELMT">
   <key>
    <column name="REQUEST_ANS_IX" />
    <column name="REQUEST_ID" />
  </key>
   <composite-element class="RequestAnswerElement">
    <property name="ownerId" type="long" column="OWNER_ID" not-null="true" length="11">
    </property>
    ...
    <many-to-one name="reportingData" class="Rptd" not-null="true" update="true" insert="true">
      <column name="RPTD_ID" />
      </many-to-one>
   </composite-element>
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 12:58 am 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Hibernate will create a record in the request_ans_elmt table for each object you add to the set, irrespective of the element type (primitive or composite). Each one of those records will have a foreign key back to the parent record, in addition to the columns specified in the element being stored. The order that you specify the columns in the <key> element in the <set> must match the order of the columns specified in the parent entity's key.

This should work OK for you, AFAIK. You may need to drop the inverse=true, since this isn't really at the inverse side of an association. Also, try cascade=save-update if you plan to delete items from the requestAnswerElements set.

It is an awkward setup, but it will work. Remember to have RequestAnswerElement implement equals and hashCode if you want to add and remove from the set.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 5:07 am 
Beginner
Beginner

Joined: Mon Apr 11, 2005 8:37 am
Posts: 22
Thanks for the reply! it helped to find the error.
The inverse=true was the problem, it does work perfectly without it.
But i think there is a little error in your post: For cascading delete cascade=all or cascade=delete is necessary.
Thanks anyway!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 8:32 am 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Sorry.... use save-update in a situation where you have a many-to-many as the elements of a collection. You are correct about cascade for one-to-many in this case.


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.