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

  • Home
  • SEARCH
  • 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 6544077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:25:22+00:00 2026-05-25T11:25:22+00:00

so here’s the situation: suppose I have a class structure used to represent flexible

  • 0

so here’s the situation: suppose I have a class structure used to represent flexible search:

public class SearchDefinition
{
    public virtual string Name {get; set;}
    public virtual IEnumerable<SearchTerm> Terms {get; set;}
}

public abstract class SearchTerm
{
    public virtual Operator Op {get; set; } //i.e 'In', 'Not in', 'Contains' etc..
    public abstract IEnumerable<object> CompareValues {get; } //the values against which the search is performed. for example- 'in (2,6,4)', 'contains ('foo', 'blah')'.
}

now, since search terms can refer to different fields, each type of term has its own class:

public class NameSearchTerm : SearchTerm
{
   public virtual IEnumberable<string> ConcreteValues {get; set;}
   public override IEnumberable<object> CompareValues 
     {
        get
        {
            return ConcreteValues.Cast<object>();
        }
     }
}

and so on, with collections of different types.
Terms are mapped using table-per-heirarchy, except for the ConcreteValues collections, which are mapped to different tables (a table for string values, a table for int values etc..).

my question is- how do I efficiently retrieve a list of SearchDefinitions? for the collection of SearchTerms I can’t use select strategy (will result in select N+1).
However, fetching using JoinQueryOver or JoinAlias, while sending the correct query, does not populate the collection:

var definitions = session.QueryOver<SearchDefinition>()
   .Where(/*condition*/)
   .JoinAlias(d=> d.Terms, () => termsAlias)
   .List();   //sends a correct, joined query which fetches also the terms from the terms table

Assert.IsTrue(NHibernateUtil.IsInitialized(definitions[0].Terms)); //THIS FAILS!

any suggestions on how to do this?
I’m adding the fluent mappings here-

the terms collection inside the SearchDefinition class:

 mapping.HasMany(x => x.Terms)
                //.Not.LazyLoad()
                .Fetch.Subselect()
                .Cascade.AllDeleteOrphan()
                .Cache.ReadWrite();

the Concrete values collection inside the IntSearchTerm class (similar for all term classes):

mapping.HasMany<int>(t=> t.ConcreteValues).Table("TermsIntValues").Element("IntValue")
                //.Not.LazyLoad()
                .Fetch.Subselect()
                .Cascade.AllDeleteOrphan();
  • 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-25T11:25:23+00:00Added an answer on May 25, 2026 at 11:25 am

    the thing is that once the fetching strategy in the mapping file is defined as ‘SubSelect’, initializing one type of collection would initialize it on all objects that contain that type of collection.
    See the follwing working code for further details:

        var subQuery = QueryOver.Of<SearchDefinition>()
            .Where(p => p.IsActive)
            .Select(p => p.Id);
    
        var searchDefinitionsQuery = Session.QueryOver<SearchDefinition>()
            .WithSubquery.WhereProperty(p => p.Id).In(subQuery);
            searchDefinitionsQuery.OrderBy(p => p.SortOrder).Asc();
    
        var searchDefinitionsWithTerms = searchDefinitionsQuery.Future();
    
        var intValuesQuery = Session.QueryOver<IntValuesTerm>()
            .WithSubquery.WhereProperty(c => c.SearchDefinition.Id).In(subQuery)
            .Future();
    
        var stringValuesQuery = Session.QueryOver<StringValuesTerm>()
            .WithSubquery.WhereProperty(c => c.SearchDefinition.Id).In(subQuery)
            .Future();
    
        var timespanValuesQuery = Session.QueryOver<TimeSpanValuesTerm>()
            .WithSubquery.WhereProperty(c => c.SearchDefinition.Id).In(subQuery)
            .Future();
    
    
        if (searchDefinitionsWithTerms.Count() == 0)
        {
            return searchDefinitionsWithTerms;
    
        }
    
        /*if the searchDefinitions collection isn't empty- make sure all collections are initialized.
         * 
         * since our fetching strategies are all 'SubSelect' (see SearchDefinitionMappingOverride, SearchDefinitionTermsMappingOverride),
         * all we need to do is inialize ONE collection of each type (intValuesTerms, string values Terms etc..), and then automatically all other collections of the same type will also be initialized.
         * (look at the generated sql query for further info).
         * for example: if we have 5 searchDefinitions, each with 1 Term of type 'IntValuesTerm', it's enough to initialize just one of those collections, and then all others of the same type will be initialized as well.
         */
    
    
        //need to initalize each type of collection (int, string, timespan) once, in order for all the collections of that type to initialize
        IntValuesTerm intTerm = (IntValuesTerm) searchDefinitionsWithTerms.SelectMany(p => p.Terms).FirstOrDefault(c => c is IntValuesTerm);
        if (intTerm != null )
        {
            NHibernateUtil.Initialize(intTerm.IntValues);
        }
    
        StringValuesTerm stringTerm = (StringValuesTerm)searchDefinitionsWithTerms.SelectMany(p => p.Terms).FirstOrDefault(c => c is StringValuesTerm);
        if (stringTerm != null)
        {
            NHibernateUtil.Initialize(stringTerm.StringValues);
        }
    
        TimeSpanValuesTerm timespanTerm = (TimeSpanValuesTerm)searchDefinitionsWithTerms.SelectMany(p => p.Terms).FirstOrDefault(c => c is TimeSpanValuesTerm);
        if (timespanTerm != null)
        {
            NHibernateUtil.Initialize(timespanTerm.TimeSpanValues);
        }
    
        return searchDefinitionsWithTerms; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is what I am trying to achieve in PHP: I have this string:
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here's the situation, i want to have a user that can enter time on
Here is my situation: I am using telerik with winform. I have a dataset
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here we go again, the old argument still arises... Would we better have a
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()

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.