Typical Object/Relational impedance mismatch :
Many to many mapping is a way to map three tables to only two
classes. Columns from COCKTAIL are mapped to Cocktail attributes,
columns from INGREDIENT are mapped to Ingredient attributes, but where
do you handle COCKTAIL_INGREDIENT columns ?
=> you need another class
=> it is not a many to many relation

I would suggest using two 1-n relations and three classes.

Hope this helps...

Elmar Seestaedt wrote:
Hello,

I have the following problem. I have a many to many relationship between two
objects, but there are Attributes specified to the relation itself.
I have the following two objects.

public class Cocktail
{
  private int id;
  private String name;
  private Collection ingredients;
  /* getters and setters */
}

public class Ingredient
{
  private int id;
  private String name;
  private Collection cocktails;
  /* getters and setters */
}

and the relationship also has the attribute amount and unit.
E.g. In a Long Island Ice Tea there is the Ingredient Vodka with 2cl.
But in Caipirowska is Vodka with 4cl.

The DB Scheme is:
CREATE TABLE COCKTAIL (
	cocktail_id INTEGER NOT NULL,
	name VARCHAR ( 100 ) NOT NULL,
	CONSTRAINT PK_COCKTAIL_cocktail_id PRIMARY KEY (cocktail_id)
	);
CREATE TABLE INGREDIENT(
	ingredient_id INTEGER NOT NULL,
	name VARCHAR ( 100 ) NOT NULL,
	CONSTRAINT PK_INGREDIENT_ingredient_id PRIMARY KEY (ingredient_id)
	);
CREATE TABLE COCKTAIL_INGREDIENT(
	cocktail_id INTEGER NOT NULL,
	ingredient_id INTEGER NOT NULL,
	amount INTEGER NOT NULL,
	unit VARCHAR ( 10) NOT NULL
	CONSTRAINT PK_COCKTAIL_INGREDIENT_cocktail_id_ ingredient_id PRIMARY KEY
(cocktail_id, ingredient_id)
	);

Thinking about it the amount and unit belongs to the relation.

I mentioned that there were some mails about this problem, but as far as I
recognized there has never been a solution.
The example with products and groups is only a many to many relationship but
without attributes at the relation.

I would be very thankful for any comments ;-)

Sincerly Elmar


--
Mickael Guessant

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev

Reply via email to