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.