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.  [ 9 posts ] 
Author Message
 Post subject: Avoiding intermediate tables with indirect self join
PostPosted: Fri Mar 31, 2006 5:13 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 7:14 pm
Posts: 28
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.2

Name and version of the database you are using:
MySQL 5.0.18

I am implementing a retail site where products have addons and warranties. Addons and warranties are really products themselves. I have intermediate tables for addons and warranties with the parent product id and the child product id.

I have it working now so the parent product can access the child product ids in the intermediate table. However, I'd like to make it so that the bag of addons could be a bag of Products instead of a bag of Addons. It would save an extra query each time I load a Product and have to display addons. The same goes for warranties.

Does anyone have experience with this indirect parent/child paradigm that could help me see how to work this?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 02, 2006 11:31 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Sure. If there's a suitable discriminator column, this isn't too hard to do. There is some ugliness because the discriminator values get hardcoded into your mapping file a couple of times..

ProductImpl gets two subclasses, AddonImpl and WarrantyImpl. Assuming that the discriminator column is "ProductTypeID" and has values 'p', 'a' and 'w', the Product mapping gets two bags:
Code:
<bag name="Addons" where="ProductTypeID='a'">
  <key column="MainProductID" not-null="true"/>
  <one-to-many class="AddonImpl"/>
</bag>

<bag name="Warranties" where="ProductTypeID='w'">
  <key column="WarrantiedProductID" not-null="true"/>
  <one-to-many class="WarrantyImpl"/>
</bag>
You should write an event handler or at least a product-validation method that ensures that the user hasn't put any warranties into the addons bag, etc.


Top
 Profile  
 
 Post subject: Possible
PostPosted: Mon Apr 03, 2006 12:19 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 7:14 pm
Posts: 28
I think that would work except that for each product, there is a unique set of addons and warranties. So I couldn't do a general where condition on the bag because it would return *all* of the addons or warranties.

If there were a way to specify a foreign key for it or simply have a bag of products with a where condition that includes a variable that could be set. It seems like that would work, but I don't think the where clause for a hibernate collection mapping supports variables in the where clause.


Top
 Profile  
 
 Post subject: Solution for my situation
PostPosted: Mon Apr 03, 2006 1:14 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 7:14 pm
Posts: 28
I think I found a solution for my situation. I can use a one-to-one, or the many-to-one mapping with unique set to true, to map the addon or warranty back to the product table with its foreign key. So in order to reference the actual product, I've setup a unique many-to-one relationship with the class being the Product class and the column being the product id itself.

Thanks for the help - your train of thought got me thinking of how to do it for my app.


Top
 Profile  
 
 Post subject: Still in need of a solution
PostPosted: Mon Apr 03, 2006 3:28 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 7:14 pm
Posts: 28
My previously mentioned solution involves still having objects of type addon or warranty in the code. I'm wondering if there is a way to do the "bag" mapping that would circumvent mapping the addon/warranty at all and instead just having a bag of the parent type - product.

Does anyone have any other suggestions for this?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 5:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use a Product interface. It won't matter if a particular bag holds just WarrantyImpls or just AddonImpls, so long as both classes implement The Product interface.


Top
 Profile  
 
 Post subject: product interface
PostPosted: Mon Apr 03, 2006 7:10 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 7:14 pm
Posts: 28
How would this filter the addons and warranties by the specific product id though? I guess that's the real obstacle for me. When I call up a particular product, I need to to get the addons and warranties for that particular product id, which is done with the intermediate relationship tables.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 7:20 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Those join tables are simple two-column things like ProductId/WarrantyId, or ProductId/AddonId, right? Doesn't the "normal" bidirectional many-to-many do what you need? That's described in section 7.5.3. "many to many" of the ref docs.


Top
 Profile  
 
 Post subject: Good idea
PostPosted: Tue Apr 04, 2006 5:32 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 7:14 pm
Posts: 28
Being fairly inexperienced with hibernate, I think that's a great idea. I have had to think through this a few times because this data is going to be completely refreshed every day. That means that every time I run it, I have to do two passes through the data - one to get all of the products, then another to save all of the addons and warranties because the surrogate product id will not necessarily have been created for the addon/warranty yet during the first pass.

I had been trying to do it with only one pass with various methods. If I knew hibernate better, I might be able to do that. It seems like the better way to do it is to make it fit better with the example that you're talking about. That makes the code simpler too. With some initial refactoring, it looks like it is going to work.

Thanks a lot for the help!


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