-->
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.  [ 5 posts ] 
Author Message
 Post subject: SQL help
PostPosted: Wed Oct 11, 2006 3:11 pm 
Newbie

Joined: Mon Jul 26, 2004 11:35 am
Posts: 13
Hello,
I have three simple tables

PRODUCT
id
name
manufacturer

FEATURE
id
name
sortorder

PRODUCT_FEATURE
id
product_id
feature_id
value

I am trying to build a search based on Product name and manufacturer and a feature value, where feature sql should be something like
((feature_id=1 and value in ("V1","v2") and (feature_id=2 and value in ("V1","v2"))

When a single feature select executed it returns results, when more than one feature selected it returns empty result set. I realized that feature_id cannot equal 1 and 2 at the same time, so my question to you SQL gurus how can I structure my sql statement to include all fields from product and product feature tables? Can I create a single view to incorporate all that data?

I am using Hibernate 3.1 rc3, with MySql, but that it is irrelevant, since my question is sql related and I don't know any other good forum to post it on
Thank you


Top
 Profile  
 
 Post subject: Re: SQL help
PostPosted: Wed Oct 11, 2006 6:32 pm 
Newbie

Joined: Wed Oct 11, 2006 5:11 pm
Posts: 4
If I understand you correctly, you want to have multiple feature results for a single product, each feature as its own column. Is that right?

If so, what you need to do is join against the tables more than once:

Code:
Select P.*, Feature1=F1.name, Feature2=F2.name
    from
        PRODUCT P,
        PRODUCT_FEATURE PF1, FEATURE F1,
        PRODUCT_FEATURE PF2, FEATURE F2
where       
        P.id = PF1.product_id and PF1.feature_id = F1.id and PF1.value in ("V1", "V2")
        and
        P.id = PF2.product_id and PF2.feature_id = F2.id and PF2.value in ("V1", "V2")



Does that give you what you need?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 9:35 am 
Newbie

Joined: Mon Jul 26, 2004 11:35 am
Posts: 13
Thank you, however I have 24 feature search criteria, does that mean I have to join PRODUCT_FEATURE 24 times?? Is there a way to create a VIEW?

Second question, how can I build this query in Hibernate either using criteria or HQL query?

Thank you for all your help
Roman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 10:33 am 
Newbie

Joined: Wed Oct 11, 2006 5:11 pm
Posts: 4
If you want them as column data then yes, you would have to do all those joins.
Preferably, you could just rely on a regular row based join and your query is much simpler.

Code:
Select P.*, FeatureName=F1.name
    from
        PRODUCT P,
        PRODUCT_FEATURE PF, FEATURE F
where       
        P.id = PF.product_id and feature_id = F.id and PF.value in ("V1", "V2")


and you'll get one row for each product/feature pairing.

If you still insist on the column based result, the only help a view could give would be a view that links the PRODUCT_FEATURE.product_id to the FEATURE.name, simplifying your query a bit. So:

Code:
create view PRODUCT_FEATURENAME
as
    select product_id, featureName=F.name
        from PRODUCT_FEATURE PF, Feature F
    where
        PF.feature_id = F.id and PF.value in ("V1", "v2" )


Then your original query gets simplified as such:

Code:
Select P.*, Feature1=PFN1.name, Feature2=PFN2.name, Feature3=PFN3.name, ... Feature=PFN24.name
    from
        PRODUCT P,
        PRODUCT_FEATURENAME PFN1,
        PRODUCT_FEATURENAME PFN2,
        PRODUCT_FEATURENAME PFN3,
        ...
        PRODUCT_FEATURENAME PFN24

where       
        P.id *= PFN1.product_id
        and
        P.id *= PFN2.product_id
        and
        P.id *= PFN3.product_id
        ...
        P.id *= PFN24.product_id


Frankly, I don't even know if this query might push the limit on how many tables you can join against. The fact that the view exists is just a convenience but at execution time will still be expanded out. You might be better off selecting the results of the view into a temp table first.


If I may ask, what is your motivation for doing this at all?

(btw, please rate my posting)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 11:17 am 
Newbie

Joined: Mon Jul 26, 2004 11:35 am
Posts: 13
Thank you for your reply. I am dealing with advanced search dilema, where a user can search based on product feature values. For example, a user may choose to select that a certain product may contain feature 1, feature 2, and so on.. By contain, I mean values in ("Std","Opt"), just an example. So for every feature I must add a join, which I guess cannot be solved using cirteria model, or can it? What does criteria.createCriteria("someAlias") method do?

I can build out HQL for each feature id add a join, but that means I have to rewrite other product search criterias. The schema I provided is the simplified version, product table contains another 20 searchable fields and three more searchable join tables...
Thanks for your help


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