Hibernate version:2.1.7c
[Name and version of the database :Oracle 8
I have the following senario and having trouble in getting records inserted into ITEM_DESPATCH table when saving an ITEM record.
Quote:
ITEM (ITEM_ID, NAME)
ITEM_DESPATCH (ITEM_ID, SERIAL_CODE , QTY)
My ITEM.hbm.xml is as follows:
Code:
<hibernate-mapping>
<!-- roomsnet.r5.dao.Item root -->
<class name="dao.Item" table="ITEM" >
<id name="id" type="string">
<column name="ITEM_ID" length="10"/>
<generator class="assigned"/>
</id>
<set name="despatches" inverse="true" cascade="save-update">
<key column="ITEM_ID"/>
<one-to-many class="roomsnet.r5.dao.ItemDespatch"/>
</set>
<property name="desc" column="NAME" type="string"/>
</class>
</hibernate-mapping>
My ITEM_DESPATCH .hbm.xml is as follows:
Code:
<hibernate-mapping>
<class name="dao.ItemDespatch" table="ITEM_DESPATCH">
<composite-id name="id"
class="dao.ItemDespatch$Id">
<key-property name="itemId" column="ITEM_ID" type="string" length="10"/>
<key-property name="serial" column="SERIAL_CODE" type="int"/>
</composite-id>
<!--property name="serial" column="serial" type="int"/-->
<many-to-one name="item" column="ITEM_ID" insert="false" update="false" not-null="true"/>
<property name="qty" column="QTY" type="int"/>
</class>
</hibernate-mapping>
The ItemDespatch DAO :
Code:
public class ItemDespatch implements Serializable
{
public static class Id implements Serializable {
private int serial;
private String itemId;
public int getSerial() {
return this.serial;
}
public void setSerial(int serial) {
this.serial = serial;
}
public String getItemId() {
return this.itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
/**
* Composite-id classes must override hashCode()
*/
public int hashCode() {
return itemId.hashCode() + serial;
}
/**
* Composite-id classes must override equals(x)
*/
public boolean equals(Object other) {
if (other instanceof Id) {
Id that = (Id) other;
return that.itemId.equals(this.itemId) &&
that.serial == this.serial;
}
else {
return false;
}
}
}
private Id id = new Id();
Item item;
private int qty;
public ItemDespatch(Item item,int serial)
{
this.item = item;
this.id.itemId = item.getId();
this.id.serial = serial;
item.getDespatches().add(this);
}
/** default constructor */
public ItemDespatch(){}
public void setId(Id id) {
this.id = id;
}
public Id getId() {
return this.id;
}
public int getQty() {
return this.qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public Item getItem() {
return this.item;
}
public void setItem(Item item) {
this.item = item;
}
public String toString() {
return " [Item Despatch] " + " Qty: "+qty;
}
}
The Item DAO :
Code:
public class Item implements Serializable
{
private String id;
private String Desc;
private Set despatches = new HashSet();
/** default constructor */
public Item(){}
public void setId(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
public String getDesc() {
return this.Desc;
}
public void setDesc(String Desc) {
this.Desc = Desc;
}
public Set getDespatches() {
return this.despatches;
}
public void setDespatches(Set despatches) {
this.despatches = despatches;
}
public String toString() {
return " [Item] " + "Code : "+ id +" Desc : "+Desc;
}
}
Code to persist ITEM and ITEM_DESPATCH :
Code:
Item item = new Item();
item.setId("ITEM10");
item.setDesc("Beans");
session.save(item);
ItemDespatch itemDespatch = new ItemDespatch(item,0002);
itemDespatch.setQty(250);
what am i missing here? No records get added to ITEM_DESPATCH table.
I had a look at
http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/cid/ too.
Please help
Shanika