This is meant as a tutorial and learning process for all including myself. When start getting into relationships its important to notice how to define them and how your going to handle them. In this demo we are going to look at a simple system that uses a product and category object. The basics of hibernate (list/get/save/delete) are pretty easy but when I get into relationships I usually have some questions. Please follow along with my thought process and provide feedback and by all means please tell me when & where I am wrong.
The first object in our system is a product object. I have slimmed down version of the product here but you get the idea.
The object in our system is a category object. The only thing of special note here is the name field. The table that already exists contains the field category and I would like to refer to it as name from here on out.
When I get a list of my products I see 100 objects that look like the one below.
If I dump out a list of categories I see a bunch of objects that look like this.
So far we have walked through the basics. Now I want to get into defining relationships. First we will look at our product object. A product can be assigned to one and only category in this system. This is the reason we decided to add the category id foreign key to the table. Bob Silverberg said something awhile back that has always stuck with me. When your deciding on what the relationship always start with the cfc your in as the origin. In other words how does this object relate to the object we are defining the relationship with.
This is where things get confusing for me. I believe the right answer to this is one-to-one but I also feel like if the other side is going to be a many-to-one that almost always makes this a one-to-many. Either way we setup our relationship below and now we can see the category object when we dump our product.
Next we will look at defining the relationship between the category and the product. Thinking in the context of this object to that 1 category object could belong to many products there for our relationship is one-to-many. So far so good right? Take a look at the output when we dump a single product now.
The product has some properties and one of them is category. That category object because of its one-to-many relationship now also has a list of product objects that belong to that category. I thought this is where lazy loading came in and because lazy defaults to true I would of thought that the products would not have been populated until I asked for them (lazy loading). If anyone knows why this does this please let me know. I probably just have my relationships setup wrong.
Pretend for a second that everything is working how its supposed to. My application is going to output a list of products. Since I have 100 products right now and more to come I don't want to dump them all to the screen. With that I have 2 options for displaying the products. I can display the products using paging (1-10/11-20/etc) or I can display the products by the category they belong to since I know for a fact the most I will see in a category is 20 products. I could find the products by category by filtering from products. Or I could grab the category that we are working with and get a list of products by that category. Both make sense to me so I guess I would pose the question to you. Is there a right answer here? Is there one that makes more sense? Is there performance issues with using either?
I am still learning but I think this little walk through will be a good one for myself and others to learn from. Please give me feedback, tell me I am wrong, tell me I am an idiot I really don't care! My thirst for knowledge exceeds my ego so let me know what you think and what you would do different.

#1 by Henry Ho on 12/18/09 - 2:47 PM
#2 by Dan Vega on 12/18/09 - 2:50 PM
#3 by Brian Swartzfager on 12/18/09 - 3:25 PM
#4 by Dan Vega on 12/18/09 - 3:32 PM
#5 by spills on 12/18/09 - 6:49 PM
#6 by Bardnet on 12/19/09 - 2:00 PM
#7 by Bob Silverberg on 12/19/09 - 3:19 PM
Regarding your Product-Category relationship, here's a question for you: How many products are allowed to be assigned to a single Category? If the answer is "more than one" (which it must be because of the one-to-many between Category and Product), then that means that Product-Category is actually a many-to-one, not a one-to-one. A one-to-one implies that a Product can only be assigned to one Category (which is true) *and* that a Category can only be assigned to one Product (which is not true).
I also think you'll run into trouble having CategoryId defined as a property in the Product object. If you have a property that represents the relationship between Product and Category (the category property in your example) you generally don't also define the foreign key that makes up that relationship as a property as well. I would expect that you'd have problems with the SQL generated in this case, unless Hibernate is smart enough to realize that both properties refer to the same thing. In either case it's not generally the way one deals with composed objects. If you need to know the categoryid associated with a Product you'd generally ask for Product.getCategory().getCategoryId().
#8 by Dan Vega on 12/20/09 - 8:51 AM
#9 by Francisco Paulino on 1/7/10 - 11:29 AM
Using your example, the following error appears:
Repeated column in mapping for entity: Product column: categoryid (should be mapped with insert="false" update="false")
#10 by Dan Vega on 1/7/10 - 12:26 PM