-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Object vs primitives in identifier object (in composite-id)
PostPosted: Mon Oct 18, 2010 10:34 am 
Newbie

Joined: Mon Oct 18, 2010 9:35 am
Posts: 10
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


Top
 Profile  
 
 Post subject: Re: Object vs primitives in identifier object (in composite-id)
PostPosted: Fri Oct 22, 2010 6:13 am 
Newbie

Joined: Mon Oct 18, 2010 9:35 am
Posts: 10
Hi,

I am guessing that this kind of complex composite-id (formed by objects/components instead of primitives) is not possible.

I changed my application model so I can counter the mapping requirements:
Code:
public class DetailedProduct implements Comparable<DetailedProduct>, Serializable {
          private DetailedProductId detailedProductId;
          private long otherDetailedProductData;
          private Product product;
          private Size size;
          private Color color;
          //constructor;setters;getters;equals;hashCode
}
public class DetailedProductPK  {
   private long productId;
   private long sizeId;
   private long colorId;
        //constructor;setters;getters;equals;hashCode
}

<class name="DetailedProduct">
             <composite-id name="detailedProductPK" class="DetailedProductPK">
                      <key-many-to-one name="productId" class="Product" column="productId"/>
                      <key-many-to-one name="sizeId" class="Size" column="sizeId"/>
                      <key-many-to-one name="colorId" class="Color" column="colorId"/>
             </composite-id>
             <many-to-one name="product" class="Product" column="productId" insert="false" update="false"/>
             <many-to-one name="size" class="Size" column="sizeId" insert="false" update="false"/>
             <many-to-one name="color" class="Color" column="colorId" insert="false" update="false"/>
             <property name="otherDetailedProductData"/>
</class>
   <class name="DetailedProduct" table="ProductSizeJunction" entity-name="ProductSizeJunctionDetailedProduct">
             <composite-id name="detailedProductPK" class="DetailedProductPK">
                     <key-many-to-one name="productId" class="Product" column="productId"/>
                     <key-many-to-one name="sizeId" class="Size" column="sizeId"/>
             </composite-id>
             <property name="width" type="float" not-null="true"/>
   </class>


The
Code:
insert="false" update="false"
part is needed because hibernate won't allow two columns with the same name to be mapped.

I don't see any other way I can map the object/component ids only once. If someone sees a problem with this solution or knows a better one, I invite you to write here.

Kind Regards,
Despot


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.