-->
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: Insert null into composite id in bidirectional one-to-many
PostPosted: Fri Feb 05, 2010 2:03 am 
Newbie

Joined: Fri Feb 05, 2010 1:05 am
Posts: 12
Hi experts, I am new to Hibernate and is using hibernate-3.3.2.GA. I am trying to implement a one to many association where the primary key of the 'one' table is one of the composite keys on the 'many' side. Below is the config files and sample code to add both the parent and child.

item (the one side)

Code:
<class name="Item" table="TAB_ITEM_DATA">
  <id column="ITEM_ID" name="itemId" type="string" >
      <generator class="sequence">                  
   </generator>      
  </id>
 
  <set name="itemText" inverse="true" lazy="true">
   <key column="ITEM_ID" />
   <one-to-many class="ItemText" />
  </set>
</class>


ItemText(the many side)
Code:
<class name="ItemText" table="TAB_ITEM_TEXT">
     
  <composite-id >       
       <key-property column="ITEM_ID" name="itemID" type="string"/>   
   <key-property column="TEXT_ID" name="textID" type="string"/>
  </composite-id>
 
<many-to-one name="itemData" class="Item" insert="false" update="false" column="ITEM_ID" not-null="true"/> 

</class>


java code
Code:
tx = session.beginTransaction();

         Item item = new Item();
         itemText text = new itemText();                           
         text.setTextID("ID_1");
         text.setTextLine("Testing");   
         text.setItemData(item);   
         item.getItemText().add(text);      
         
         session.save(item);
         session.flush();         
               
         session.save(text);
         session.flush();                                    
         tx.commit();


The config seemed to work (as no errors prompted). However, using the java code provided above, hibernate insert the itemText with NULL ITEM_ID, which supposed to be the primary key of the Item table. I can manually call text.setItemId() to add the item id but I do not think it's the right approach though.
So, please help me to point out if there's any configuration changes needed.

p.s. I cannnot implement the same relation in a unidirectional way as well, if there's anyone willing to help, please also provide some examples for it.


Top
 Profile  
 
 Post subject: Re: Insert null into composite id in bidirectional one-to-many
PostPosted: Fri Feb 05, 2010 3:27 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
When you are using a composite id you need to set the values yourself. Have you tried using <key-many-to-one> to get rid of the duplicate mapping for the ITEM_ID column? There are a few examples here: http://docs.jboss.org/hibernate/stable/ ... ompositeid


Top
 Profile  
 
 Post subject: Re: Insert null into composite id in bidirectional one-to-many
PostPosted: Fri Feb 05, 2010 3:45 am 
Newbie

Joined: Fri Feb 05, 2010 1:05 am
Posts: 12
Thanks for the reply nordborg.
Yes I did try both <many to one> and <key many to one> approach on "auto id setting" but got no luck.
So do you mean hibernate would not be able to set the composite id automatically when the parent.setChildren() or child.setParent() are called?

Is there any possible workaround on this?
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Insert null into composite id in bidirectional one-to-many
PostPosted: Fri Feb 05, 2010 3:59 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The way you have mapped it you have told Hibernate to ignore everything but the <key-property column="TEXT_ID" ..> mapping. In your <set> with inverse="true" which means hibernate will ignore it, and you have mapped the <many-to-one> with insert="false" update="false" which means that Hibernate will ignore it. But it will not help even if you change that because you are required to populate the composite id yourself. See http://docs.jboss.org/hibernate/stable/ ... ompositeid for more information.

What do you mean with <key many to one> approach on "auto id setting"?


Top
 Profile  
 
 Post subject: Re: Insert null into composite id in bidirectional one-to-many
PostPosted: Fri Feb 05, 2010 4:38 am 
Newbie

Joined: Fri Feb 05, 2010 1:05 am
Posts: 12
Yes I have read the user manual already but couldnt find anything I want. :(

The "auto id setting" mean hibernate automatically sets the ids for me if I call parent.addchildren() or children.setparent(). I tried all three composite id approach but none helped on this issue.
So if there's no workaround on this then I am afraid I have to stick with the manual setting of composite id apporoach, which I think violates what hibernate expected us to do:/

Thanks.

p.s. I didnt use the key-many-to-one as I have read from somewhere that it's not well supported.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.