Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6023737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:02:59+00:00 2026-05-23T04:02:59+00:00

I have a database schema, where I have a Product , Category , CategoryFeature

  • 0

I have a database schema, where I have a Product, Category, CategoryFeature, and an ProductCategoryFeatureValue.

The Model is mapped using Fluent NHibernate, but basically is as follows.

Product
-------
ID
Title

Category
--------
ID
Title

CategoryFeature
---------------
ID
CategoryID
Title

ProductCategoryFeatureValue
---------------
ID
ProductID
CategoryFeatureID
_______________________

Category [one] <-> [many] CategoryFeature
Product [many] <-> [many] ProductCategoryFeatureValue

Basically, the features available to a product are listed in the ProductCategoryFeatureValue table, which is the ‘middle-link’ for the many-to-many collection.

I need to create a query, where i can find all products, which have ALL the features selected by the user.

Example, doing a search for two features with ids 643229 & 667811 in SQL terms, I would do something like this:

SELECT * FROM Product 
JOIN ProductCategoryFeatureValue AS feature1 ON Product.id = feature1.ProductID AND     feature1.categoryfeatureid = 643229
JOIN productcategoryfeaturevalue AS feature2 ON Product.id = feature2.ProductID AND feature2.categoryfeatureid = 667811

Another query which I could do is this:

SELECT * FROM product WHERE 
((SELECT id FROM productcategoryfeaturevalue AS feature1 WHERE feature1.ItemGroupID = product.id AND feature1.categoryFeatureID = 643229 LIMIT 1) IS     NOT NULL)
AND 
((SELECT id FROM productcategoryfeaturevalue AS feature2 WHERE feature2.ItemGroupID = product.id AND feature2.categoryFeatureID = 667811 LIMIT 1) IS NOT NULL)

Both have been tested and work well. However, I cannot seem to reproduce them using NHibernate. Any ideas?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:03:00+00:00Added an answer on May 23, 2026 at 4:03 am

    I believe you want something like this in SQL

    Select *
    from Products p
    where p.id in (
       select fv.ProductId
       from ProductCategoryFeatureValue fv
       where fv.CategoryFeatureID in (643229,643230)
       group by fv.ProductId
       having count(*)=@NumberOfDistinctFeaturesSelected 
    )
    

    That will stop you having to JOIN to the ProductCategoryFeatureValue table multiple times for every feature selected by the user. At the very least your going to get a nicer query plan. If you don’t like the IN clause you could also use a temp table instead.

    In terms of translating this into NHibernate it doesn’t support any HAVING clause logic in the Criteria API but it is supported using HQL.

    HQL Examples

    var results = session.CreateQuery("from Product p where p.Id in (
        select fv.Product.Id
        from ProductCategoryFeatureValue fv
        where fv.CategoryFeature.Id in :featureids
        group by fv.Product.Id
        having count(fv)=:features
    )")
    .SetParameter("featureids", arrayOfFeatureIds)
    .SetParameter("features", arrayOfFeatureIds.Count)
    .List<Product>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never designed a database/data-model/schema from scratch, especially for a web-application. In some
We have a product using Oracle, with about 5000 objects in the database (tables
I have a database schema in SQL 2005 that I want to copy to
I have a database schema where the convention for a foreign key's name is:
I have a database schema that is similar to the following: | User |
So I have a database schema like this: Users UserId RoleUserXRef RoleUserId RoleId UserId
I am new to linqtosql. I have a database schema where, An employee can
Lets imagine I have the same database schema as here: http://www.databaseanswers.org/data_models/driving_school/index.htm If a customer
I have a pretty complex database schema and would like to know if there
I have no control over database schema and have the following (simplified) table structure:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.