-->
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.  [ 3 posts ] 
Author Message
 Post subject: how to implement a parent child one-to-many relationship
PostPosted: Fri Oct 19, 2007 1:01 pm 
Newbie

Joined: Tue Oct 16, 2007 3:42 pm
Posts: 11
hi,

i'm trying to implement a parent child relationship (one-to-many) relationship between two tables using hibernate annotations in the POJOs of the two relevant tables.

i'm new to this and any guidance will be helpful. say for example, my tables are "Seller" and "Items". A seller can have many items associated with it, but an Item can be associated to only one seller. The Seller POJO contains its PK, FirstName, LastName. The Item POJO contains its PK, the price of the item and the PK of the parent as its FK.

i have already implemented the regular annotations like @hibernate.class stating the table before the public class statement, the @hibernate.key-property to define PKs in both parent and child, and also the @hibernate.property tag for the properties like FirstName, LastName in parent, and the price and the FK of the parent in the child.

i need some help as to how to implement the parent child relationship in this scenario. where do the annotations need to be added? do i need to add any other methods in the child table, like getItems() and add the annotations to that? any guidance with how to proceed here will be helpful. thanks!!


Top
 Profile  
 
 Post subject: answer maybe
PostPosted: Fri Oct 19, 2007 3:25 pm 
Newbie

Joined: Mon Aug 13, 2007 11:54 am
Posts: 6
I'm not good with annotation for hibernate but I do know the mappings, so maybe I can help.

First your objects need to have the foriegn keys setup in the db. Then your objects need to have the relationship in them. So something like this is needed in your seller class.



Code:
public class Seller {

   public List<ItemClass> items;

   public Invoice() {
      items = new LinkedList<ItemClass>();
   }

   public List<ItemClass> getItems() {
      return items;
   }

   public void setItems(List<ItemClass> payments) {
      this.items = items;
   }
}


this is the mapping I would use for that not sure how the annotation would look but should be similar

Code:
<bag name="items" lazy="false" inverse="true"
         cascade="all">
         <key column="sellerNum" /> <-------foriegn key column
         <one-to-many
            class="ItemsClass" />
</bag>


later on you could do the many-to-one so that you can get an items seller.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 11:35 am 
Newbie

Joined: Tue Oct 16, 2007 3:42 pm
Posts: 11
thanks a lot for that. actually i found one good example last week in the hibernate documentation, and i tried to follow it and implement it in the same way. i then compared it with your mappings above and mine are along the same lines as yours. thank you once again!


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