-->
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: Urgent Help Need: One to Many mapping.
PostPosted: Sat Dec 13, 2003 7:44 pm 
Newbie

Joined: Tue Sep 16, 2003 4:45 pm
Posts: 14
Hi There,
I am under a deliverable deadline and I am having problems with my one to many mappings.

I have a parent table order with key orderid and purchaseditems table with foreign key orderid.

Problem: When I try to query the Order table, it fetches the associated purchaseditems once, in the process also updates the orderid in purchaseditems table with 0.
Hence the consecutive queries dont work.
Given below are the mappings please suggest what should I do.

Any help would be highly appreciated.
Thanks

-----------------------------------------------------

Mapping for Order class
-----------------------------------------------------
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="Order"
table="order">


<id
name="orderid" type="int" column="orderid">
<generator class="identity"/>

</id>

<set name="purchaseditems"  lazy="true">
    <key column="orderid"/>
    <one-to-many class="PurchaseItems"/>
</set>
</class>

</hibernate-mapping>

-------------------------------------------------------------
Class definition of Order
-------------------------------------------------------------

Code:
public class Order
{
private Set purchaseitems;
private int orderid;

public void setOrderid(int torderid)
{
   this.orderid=torderid;
}

public int getOrderid()
{
        return this.orderid;
}
public void setPurchaseditems(java.util.Set v)
{
   this.purchaseditems=v;
}
public Set getPurchaseditems()
{
   return this.purchaseditems;
}

}

------------------------------------------------------------------------
class definition of Purchaseditems
------------------------------------------------------------------------
Code:
public class PurchasedItems
{
public void setItemid(java.lang.String titemid)
{
        this.itemid=titemid;
}

public String getItemid()
{
        return this.itemid;
}
public int getOrderid()
{
   return this.orderid;
}

public void setOrderid(int id)
{
   this.orderid=orderid;

}
}

---------------------------------------------------------------------
Mapping of PurchasedItems
--------------------------------------------------------------------
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="PurchasedItems" table="purchaseditems">
<property
name="itemid"
column="itemid"
/>
<property
name="orderid"
column="orderid"
/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 13, 2003 7:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use the Hibernate log and your debugger. There doesn't seem to be anything especially bad about your mappings.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 13, 2003 7:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
P.S. I can tell that this is not the actual code you are using.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Sat Dec 13, 2003 8:40 pm 
Newbie

Joined: Tue Sep 16, 2003 4:45 pm
Posts: 14
Thanks a lot Gavin for the prompt response.
Why would you say its not the code, I am using?

When I run the code, no exceptions are being thrown.
Why would it update the child table?.
It prints the details of the purchaseditems table. It shows the itemid fine but shows orderid as 0 though the actual value would be like 10 or 15.
I dont know why that is happening.

Following is the code segment for the calling controller.
SessionFactory sessionFac=cfg.buildSessionFactory();
Session sess=sessionFac.openSession();

Transaction tx=null;
tx=sess.beginTransaction();
//Debug.print(strquery);
List list=sess.find("from Order as ord where ord.orderid=15");
if(!list.isEmpty())
{
Iterator iter=list.iterator();
while(iter.hasNext())
{
Order order=(Order)iter.next();
HashSet set=new HashSet(order.getPurchaseditems());
Iterator it=set.iterator();
while(it.hasNext())
{
PurchasedItems puritems=(PurchasedItems)it.next();
System.out.println("itemid"+puritems.getItemid());--->This is fine.
System.out.println("orderid"+puritems.getOrderid());-->Prints 0
}

}
}
tx.commit();
sess.close();
sessionFac.close();


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 13, 2003 8:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Why would you say its not the code, I am using?


Because its too simple and it doesn't compile.

Especially, where are the instance variable definitions?? Are you sure one of them is not accidently static or something?


I am confident that you will be able to resolve this easily by using your debugger and watching the Hibernate log.

Put a breakpoint in the setter for orderid, for example. (Put the Hibernate src in your sourcepath.)


Top
 Profile  
 
 Post subject: Re:
PostPosted: Sat Dec 13, 2003 8:57 pm 
Newbie

Joined: Tue Sep 16, 2003 4:45 pm
Posts: 14
Thanks a lot Gavin.
I didnt give the entire code just the snapshot of it, just to make things easier for the viewer.

I highly appreciate your help.


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.