-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Mapping Problem
PostPosted: Thu Jul 14, 2005 11:38 pm 
Newbie

Joined: Thu Jul 14, 2005 2:07 am
Posts: 2
Location: India
I have the follwing entities

1. Category
2. Sub- Category
3. Product
4. Attributes
5. Item

Category may contain zero or many sub-cats.
Sub-category may contain zero or more products.
Product have the many to many relation with Attributes.
and Product may contain the zero or many items.

Here the problem comes. Here the Item is like an instance of the Product.so it contains the values of the product attributes.

I have created tables Product , Attribute and Product_Attribute. Now when it comes to Item i am not able to proceed.(how to map)

I think hibernate users will help me in this.

Thanks in advance.

Rao.

Hibernate version: 2.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
Rao


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 10:09 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
If I understood you well, your Item is in fact a Product. So it means that a Product contain a one-to-many relationship to a set of productS, if only a Item-Product can only be part of ONLY ONE Product. In this case you only have to add an extra column inside Product telling wich Item-Product is is the child a a Parent-Product : "itemForeignKeyId" (pointing on ProductId).

In the other case, if an Item-Product can be part of many Products at the same time, you need to have a many-to-many relationship (and a database table item-product linking all Item-Product to a Product bu the ProductId key).

Lets check the simpler case:

You just have to create a Set (or any collection) of "items" in you Product class. This Set will be linked to a set of "itself"... Products.

in the java code:

Code:
Set item = new HashSet();


Inside the Product mapping xml add :

Code:
<set name="items">
      <key>
        <column name="productId" not-null="false" />
      </key>
      <one-to-many class="Product" />
</set>


You can also make the inverse relationship many-to-one inside Product to another Product.

Code:
<many-to-one name="ParentProduct" class="Product">
      <column name="itemForeignKeyId" not-null="false" />
</many-to-one>



This seems to me the easiest way.

For some transparency, you could also make a class Items heriting of Product and adding the Set of Items, and the getter/setter associated to it.

I also see that you subCategory could also be a Category pointing to itself... you could do the same pattern there also.

Hope it helped.

Etienne
Montreal


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 10:22 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Here some corrections.

Code:
Set items = new HashSet();


and not Set item = new HashSet();


Code:
<many-to-one name="parentProduct" class="Product">
      <column name="itemForeignKeyId" not-null="false" />
</many-to-one>


and not

<many-to-one name="ParentProduct" class="Product">
<column name="itemForeignKeyId" not-null="false" />
</many-to-one>

and add this field:

Code:
Product parentProduct;


inside your Product class.

Dont forget the equals() and hashCode() function for all your classes, and the addItem(Product item); in the Product class if you want to add a item to a Product...

Etienne.

[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.