-->
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.  [ 6 posts ] 
Author Message
 Post subject: One-to-many mapping with a composite key
PostPosted: Wed Jul 21, 2004 7:39 am 
Newbie

Joined: Tue Jul 20, 2004 6:04 am
Posts: 4
Hi
I am using hibernate version 2.1 with DB2 ver 8.1 FP3.
I have two mapping files namely test_order and test_vehicle.
test_order
Code:

<hibernate-mapping>
    <class name="com.dcx.news.test.jdo.TestOrder" table="test_Order" >
       <composite-id  name="m_dealerOrderPK" class="com.dcx.news.test.jdo.DealerOrderPK" unsaved-value="any">
           <key-property name="orderNo" column="order_no" type="long" />
           <key-property name="dealerNo" column="dealer_no" type="java.lang.String" />
       </composite-id>
        <property name="data" column="data" type="java.lang.String"/>

      <!--  one to many asssociation -->
        <set name="vehicles" inverse="true" cascade="all" outer-join="false">
           <key>
              <column name="order_no" />     
              <column name="dealerno"/>
               </key>
           <one-to-many class="com.dcx.news.test.jdo.TestVehicle" />
      </set>
      
    </class>       
</hibernate-mapping>



test_vehicle
Code:

<hibernate-mapping>
    <class name="com.dcx.news.test.jdo.TestVehicle" table="test_Vehicle" >
      <id  name="vehicleid" column="vehicleid" type="long" unsaved-value="any">
             <generator class="increment" />
          </id>   
      <property name="dealerNo" column="dealerno" type="java.lang.String" />
       <property name="orderNo" column="order_no" type="long" />
        <!--  many to one asssociation -->
       <many-to-one name = "order" class="com.dcx.news.test.jdo.TestOrder" not-null="true"
                         update="false" insert="false"  >
             <column name="order_no" />
             <column name="dealerno" />
       </many-to-one> 
     
    </class>       
</hibernate-mapping>



My JDO classes are as follows :

com.dcx.news.test.jdo.TestOrder
Code:

package com.dcx.news.test.jdo;

import java.io.Serializable;
import java.util.Set;

public class TestOrder implements Serializable {

   private DealerOrderPK m_dealerOrderPK;
   
   private String data;

   private Set vehicles;
         
   public TestOrder() {
   }

   public String getData() {
      return data;
   }

   public void setData(String string) {
      data = string;
   }

   public DealerOrderPK getM_dealerOrderPK() {
      return m_dealerOrderPK;
   }

   public void setM_dealerOrderPK(DealerOrderPK orderPK) {
      m_dealerOrderPK = orderPK;
   }

   public Set getVehicles() {
      return vehicles;
   }

   public void setVehicles(Set set) {
      vehicles = set;
   }
}



The class com.dcx.news.test.jdo.DealerOrderPK is as follows
Code:

package com.dcx.news.test.jdo;
import java.io.Serializable;

public class DealerOrderPK implements Serializable {
   private String dealerNo;
   
   private Long orderNo;
   

   public DealerOrderPK() {

   }

   public DealerOrderPK(String dealerNo, Long orderNo)
   {
      this.dealerNo = dealerNo;
      this.orderNo = orderNo;
   }

   public String getDealerNo() {
      return dealerNo;
   }

   public Long getOrderNo() {
      return orderNo;
   }

   public void setDealerNo(String string) {
      dealerNo = string.trim();
   }

   public void setOrderNo(Long long1) {
      orderNo = long1;
   }
   
   public boolean equals(Object arg0) {
      return super.equals(arg0);
   }
   public int hashCode() {
      return super.hashCode();
   }
}



com.dcx.news.test.jdo.TestVehicle
Code:

package com.dcx.news.test.jdo;

import java.io.Serializable;

public class TestVehicle implements Serializable {

   private Long orderNo;
   
   private String dealerNo;
   
   private long vehicleid;
   
   private TestOrder order;

   public String getDealerNo() {
      return dealerNo;
   }

   public long getVehicleid() {
      return vehicleid;
   }

   public void setDealerNo(String string) {
      dealerNo = string.trim();
   }

   public void setVehicleid(long l) {
      vehicleid = l;
   }

   public TestOrder getOrder() {
      return order;
   }

   public void setOrder(TestOrder order) {
      this.order = order;
   }

   public Long getOrderNo() {
      return orderNo;
   }

   public void setOrderNo(Long long1) {
      orderNo = long1;
   }
}




The code that I am using to run this is
Code:

            SessionFactory m_sessionFactory = new Configuration().configure("/com/dcx/news/test/configuration/hibernate.cfg.xml").buildSessionFactory();
            Session m_session = m_sessionFactory.openSession();
         Transaction tx = m_session.beginTransaction();
         TestOrder m_testOrder = new TestOrder();

         m_testOrder.setM_dealerOrderPK(new DealerOrderPK("D23",new Long(1)));
         m_testOrder.setData("OrderNumber 20");

         Set m_vehicles = new HashSet();
         TestVehicle m_TestVehicle1 = new TestVehicle();

         m_TestVehicle1.setOrderNo(new Long(1));
         m_TestVehicle1.setDealerNo("D23");
         m_TestVehicle1.setOrder(m_testOrder);

         TestVehicle m_TestVehicle2 = new TestVehicle();
         m_TestVehicle2.setOrderNo(new Long(1));
         m_TestVehicle2.setOrder(m_testOrder);
         
         m_vehicles.add(m_TestVehicle1);
         m_vehicles.add(m_TestVehicle2);         
         
         m_TestVehicle1.setOrder(m_testOrder);         
         m_TestVehicle2.setOrder(m_testOrder);                  
         
         m_testOrder.setVehicles(m_vehicles);
         
         m_session.saveOrUpdate(m_testOrder);

         tx.commit();
         m_session.close();


This code works perfectly fine as it adds a record in the "test_Order" table and two corresponding records in the "test_vehicle" table.
But the problem is that when I use the following code to fecth an order it's corresponding vehicles are not fetched

Code:

            SessionFactory m_sessionFactory = new Configuration().configure("/com/dcx/news/test/configuration/hibernate.cfg.xml").buildSessionFactory();
            Session m_session = m_sessionFactory.openSession();

         Query m_query =   m_session.createQuery(" FROM TestOrder where dealer_no=:abc");
         m_query.setString("abc", "D23");

         List l1 = m_query.list();
         Iterator it = l1.iterator();                  
         while(it.hasNext())
         {
            TestOrder m_testOrder= (TestOrder)it.next();
            Set m_set = m_testOrder.getVehicles();

            System.out.println(m_set.size());         // this is always 0 but it should be 2 in this case as we have two vehicle sfor this order
            Iterator m_it = m_set.iterator();
               
            while(m_it.hasNext())            
            {
                  TestVehicle m_testVehicle = (TestVehicle)m_it.next();
                  System.out.println(m_testVehicle.getOrderNo());
                  System.out.println( m_testVehicle.getOrder().getData());
            }   
         }
         m_session.close();




If I manually execute the queries generated by Hibernate then I get the actual results.
Please help.[/code]

_________________
Vipan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 7:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
there is a recent topic about this, check topics by date

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 8:22 am 
Newbie

Joined: Tue Jul 20, 2004 6:04 am
Posts: 4
Hi
I searched through the recent posts but could not find any problem similar to mine. Could you please tell me about the post you are referring to.

_________________
Vipan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 10:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
My JDO classes


what do you mean by jdo classes?

i cannot see hashcode & equals implementation.... you must do it, and do it well.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 10:52 am 
Newbie

Joined: Tue Jul 20, 2004 6:04 am
Posts: 4
by JDO classes I was referring to the com.dcx.news.test.jdo.TestOrder and com.dcx.news.test.jdo.TestVehicle classes(I have already posted their code).
I have implemented the hashcode & equals methods as you suggested but it still doesn't work e.g I implemented the equals for com.dcx.news.test.jdo.TestOrder as
Code:

if ( !(other instanceof TestOrder) ) return false;
      return (( (TestOrder) other ).getDealerNo().equals(dealerNo) && ( (TestOrder) other ).getOrderNo().equals(orderNo));



:(.

_________________
Vipan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 1:41 am 
Newbie

Joined: Tue Jul 20, 2004 6:04 am
Posts: 4
Thanks Anthony...I implemented hashcode and equals properly and it worked.. thanks a lot....

_________________
Vipan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.