Hi to all,
I was searching the forum but I couldn't find a post where the identifier object (used in the composite-id) has objects that in turn hold the ids. I always see an example were the identifier object has the ids as primitives.
Can anyone tell me how this mapping configuration would look like?Here is the concrete case. The Product class:
Code:
public class Product implements Comparable<Product>, Serializable {
private long id;
private long otherProductData;
//constructor;setters;getters;equals;hashCode
}
The Size class:
Code:
public class Size implements Comparable<Size>, Serializable {
private long id;
private long otherSizeData;
//constructor;setters;getters;equals;hashCode
}
The Color class:
Code:
public class Color implements Comparable<Color>, Serializable {
private long id;
private long otherColorData;
//constructor;setters;getters;equals;hashCode
}
The DetailedProduct class:
Code:
public class DetailedProduct implements Comparable<DetailedProduct>, Serializable {
private DetailedProductId detailedProductId;
private long otherDetailedProductData;
//constructor;setters;getters;equals;hashCode
}
The DetailedProductId class:
Code:
public class DetailedProductId implements Serializable {
private Product product;
private Size size;
private Color color;
private long otherDetailedProductData;
//constructor;setters;getters;equals;hashCode
}
I have the following for the tables:
Code:
CREATE TABLE Product (
`id` int(11) NOT NULL auto_increment,
`otherProductData` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
) ;
CREATE TABLE Size (
`id` int(4) NOT NULL auto_increment,
`otherSizeData` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE Color (
`id` int(4) NOT NULL auto_increment,
`otherColorData` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE DetailedProduct (
`productId` int(11) NOT NULL,
`sizeId` int(4) NOT NULL,
`colorId` int(4) NOT NULL,
`otherDetailedProductData` int NOT NULL,
PRIMARY KEY (productId,sizeId,colorId),
FOREIGN KEY (productId)
REFERENCES Product(id)
ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (sizeId)
REFERENCES Size(id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (colorId)
REFERENCES Color(id)
ON UPDATE CASCADE ON DELETE RESTRICT
) ;
The hibernate mapping file for DetailedProduct would look something like this:
Code:
<class name="DetailedProduct">
<composite-id name="detailedProductId" class="DetailedProductId">
<key-property name="product.id"/>????????
<key-property name="size.id"/>???????????
<key-property name="color.id"/>??????????
</composite-id>
<property name="otherDetailedProductData"/>
....
</class>
Is there a mapping configuration for this design of classes and tables?
If not, should I create the DetailedProductId to contain the ids as primitives (productId, sizeId, colorId instead of product, size, color objects) and on the other side have the objects Product, Size and Color in the DetailedProduct? That is achievable, but presents the problem of duplicating data (duplicating the productId, sizeId and colorId in both DetailedProduct and DetailedProductId).The last approach I could think of is having a separate id for the DetailedProduct, but I would rather not do that.
Kind Regards,
Despot