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: One-to-Many mapping with composite primary key :
PostPosted: Thu Dec 09, 2004 6:01 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 5:31 am
Posts: 31
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 11:42 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
change composite-id name - it can't be id - id is reserved in hibernate

regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 12:13 pm 
Newbie

Joined: Wed Sep 22, 2004 10:20 am
Posts: 5
Location: Romania
You forgot to call save() on the child item.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 10, 2004 1:01 am 
Beginner
Beginner

Joined: Fri Jun 25, 2004 5:31 am
Posts: 31
thanks guys.

I tried changing the composite-id name but didn't see any difference. Only The ITEM table gets populated.

I don't want to call save() on child explicitely. The whole intention is to get the child table populated just by calling save on the parent (ITEM).

am i missing something here?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 1:38 pm 
Newbie

Joined: Thu Dec 09, 2004 12:29 pm
Posts: 10
I think you need to call SAVE after the ItemDispatch code.

your code:

Item item = new Item();
item.setId("ITEM10");
item.setDesc("Beans");
session.save(item);
ItemDespatch itemDespatch = new ItemDespatch(item,0002);
itemDespatch.setQty(250);


My suggestion:

Item item = new Item();
item.setId("ITEM10");
item.setDesc("Beans");

ItemDespatch itemDespatch = new ItemDespatch(item,0002);
itemDespatch.setQty(250);
item.getDispatches().add(itemDespatch);

session.save(item);


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