First off, please don't flame me becasue I'm sure this question has been asked before. I have searched the forum as best I can but i'm not sure what to search for exacty. I have the book 'Hibernate in Action', I've read the FAQ, I've looked at teh documentation and I've about given up thinking that I just don't have the aptitude to solve this problem.
I'm a good database designer and a good java programmer, it's just that a friend told me I should be using Hibernate and after reading the HiA book, I agree that it would simplify my data access layer.
My problem is that I'm stuck trying to create what I believe to be two one directional one to many relationships. Here is my database schema:
Code:
CREATE TABLE Recipe (
recipeID bigint unsigned NOT NULL AUTO_INCREMENT,
name varchar(50),
PRIMARY KEY (recipeID)
)
CREATE TABLE Ingredient (
ingredientID bigint unsigned NOT NULL AUTO_INCREMENT,
name varchar(20),
PRIMARY KEY (ingredientID)
)
CREATE TABLE RecipeIngredient (
recipeIngredientID bigint unsigned NOT NULL AUTO_INCREMENT,
recipeID bigint unsigned NOT NULL,
ingredientID bigint unsigned NOT NULL,
amount varchar(20),
PRIMARY KEY (recipeIngredientID),
CONSTRAINT FK_recipe FOREIGN KEY (recipeID) REFERENCES Recipe(recipeID) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FK_ingredient FOREIGN KEY (ingredientID) REFERENCES Ingredient(ingredientID) ON DELETE CASCADE ON UPDATE CASCADE
)
My domain model is just as simple. I've got a Recipe, Ingredient and RecipeIngredient class. The Recipe and Ingredient classes hold sets of RecipeIngredient objects and the RecipeIngredient objects hold single references to the Recipe and Ingreident classes.
I've set up the db this way so I can search in either direction, I can find out what ingredients are in a recipe and also search reciepes by ingredients. The join table is there to connect the many to many and also has a property 'amount' .
This situation looks to me like the one to many / many to one bidirectional association with join table (8.5.1.) in the documentation, but I haven't been able to implement it for 2 reasons. First I don't know how to add in the 'amount' property and second I'm not sure how the domain objects should be modeled.
I'm sure most of you are looking at this question and groaning since either you've seen it 100 times before or it's just too simple to bother. But, please understand that I have literally been trying to get this to work for close to 2 days now and I've tried any number of combinations. As for reading the book, I've tried, but it's a bit too complex for me to follow.
Any help would be appreciated.