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_vehicleCode:
<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.TestOrderCode:
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.TestVehicleCode:
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]