Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Mapping data from one POJO Class to multiple tables?!
PostPosted: Mon Oct 18, 2010 3:31 pm 
Newbie

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

I cannot find any attribute in the <class> or <property> element that will allow me to map the data from an indirectly related table to a variable in a Class.

I have the following database design:
Image
ProductSizeJunction table is needed for the many-to-many relationship between Product and Size.
If I have the following class - DetailedProduct class:
Code:
public class DetailedProduct implements Comparable<DetailedProduct>, Serializable {
   private long id;
   private Product product;
   private Size size;
   private Color color;

   private float width;
   private float length;
   private float height;
   private float weight;
   private float girth;

   private long otherDetailedProductData;
   //constructor;setters;getters;equals;hashCode
}

How can I map the variables width, length, height, weight, girth to the columns with the same name from the table ProductSizeJunction?
As you can see in the following mapping file, the DetailedProduct Class is tied to the DetailedProdcut table which in turn is indirectly related to ProductSizeJucntion table, but I don't know how to specify that:
Code:
   <class name="DetailedProduct" table="DetailedProduct">
      <id name="id" column="id">
         <generator class="native" />
      </id>
      <many-to-one name="product" class="Product" column="productId"/>
      <many-to-one name="size" class="Size" column="sizeId"/>
      <many-to-one name="color" class="Color" column="colorId"/>
      <property name="otherDPData" type="string" not-null="true"/>
      <property name="width" type="float" ?????????????????????? />
   </class>


If I think of the database design, I always get to the point where I cannot merge the DetailProduct and the ProductSizeJunction because that way the database wouldn't be normalized. On the other side if I would use a plain JDBC SQL, I could insert or select the width through the productId and sizeId. How do I do it through Hibernate? Or maybe I have some mistake in the deduction!? Anyway, I would appreciate it a lot if someone gives me a tip to where I can read about this. Till now I had no luck with the reference (or I don't know where to look at). :(

The rest of the classes, although they bear no importance in this case, here they are:

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
}

Kind Regards,
Despot


Top
 Profile  
 
 Post subject: Re: Mapping data from one POJO Class to multiple tables?!
PostPosted: Tue Oct 19, 2010 11:33 am 
Newbie

Joined: Fri Jan 23, 2004 4:02 am
Posts: 6
Location: Linz, Austria
You need to do a join of DetailedProduct and ProductSizeJunction. I think you can do that using a secondary table mapping. In JPA there is a concept of secondary tables - don't know if Hibernate is using different terms. The fact that you have a primary key that is composed of multiple attributes might make that a little bit more complex than if you only had a simple surrogate key.


Top
 Profile  
 
 Post subject: Re: Mapping data from one POJO Class to multiple tables?!
PostPosted: Fri Oct 22, 2010 6:45 am 
Newbie

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

Thank you mahrer for responding.

When going through the reference book, I didn't understand that the entity-name attribute is actually a logical name: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-class
Than I found out about this: http://docs.jboss.org/hibernate/core/3.2/reference/en/html/mapping.html#mapping-entityname and I solved my problem. The solution is here:
viewtopic.php?f=1&t=1007622&p=2437356#p2437356

Kind Regards,
Despot


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 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.