-->
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.  [ 1 post ] 
Author Message
 Post subject: Simple one-to-many question
PostPosted: Tue Nov 09, 2004 1:09 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I'm in the business of wrenching my brain over to ORM-style thinking, away from SQL, and this is one of the questions I'm stumbling over...

I have a Category class and a Product class, with a many-to-one relationship between them. A Product belongs to one and only one Category, and a Category thus 'contains' many Products. So, to model this I ensure that my Product class has a Category member and the following XDoclet tag ensures that Hibernate can create the schema correctly.

/**
* @hibernate.many-to-one
* column="category_id"
* class="testing.Category"
*/

public Category getCategory() {
return category;
}

My question is do I NEED to have anything at the other end, i.e., in the Category class? At the moment I have a List member, products, and the following get method with XDoclet tag:

/**
* @hibernate.bag name="products"
* cascade="save-update"
* lazy="true"
* inverse="true"
*
* @hibernate.collection-key
* column="category_id"
*
* @hibernate.collection-one-to-many
* class="testing.Product"
*/
public List getProducts(){
return products;
}

What does this buy me? I ask because if I want to get a list of products in the category, I can simply do a find with:

from testing.Product p where p.category=?

That is, I don't need to use the List at all. I have struggled to get my head around this. Is it required for when I am approaching things from the Category end, i.e., adding Products to a Category?

I'll go back and read the reference again, but any clarification would be welcome.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.