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.
|