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 8924937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:33:38+00:00 2026-06-15T07:33:38+00:00

I have a table holding hierarchical data that I am querying for from my

  • 0

I have a table holding hierarchical data that I am querying for from my web app using NHibernate. It’s pretty straight forward

CREATE TABLE [dbo].[Relationships](
    [RelationshipId] [uniqueidentifier] NOT NULL,
    [ParentId] [uniqueidentifier] NULL,
    [ParentTypeId] [uniqueidentifier] NULL,
    [ChildId] [uniqueidentifier] NOT NULL,
    [ChildTypeId] [uniqueidentifier] NOT NULL
)

Now to query information from this table I’m using a feature of SQL Server (since 2005) called Common Table Expressions, or CTE for short. It lets me write recursive queries which are extremely useful for table like the one above.

WITH Ancestors(RelationshipId, ParentId, ChildId) 
AS 
(
    SELECT r.RelationshipId, r.ParentId, r.ChildId 
        FROM Relationships r 
        WHERE ChildId = :objectId 
    UNION ALL 
    SELECT r.RelationshipId, r.ParentId, r.ChildId 
        FROM Relationships r 
            INNER JOIN Ancestors 
                ON Ancestors.ParentId = r.ChildId 
) 
SELECT RelationshipId, ParentId, ChildId FROM Ancestors

Now, this is great and the performance isn’t that bad but it can be taxing when I try to use this to determine ancestors going up the tree, or worse, using a similar query to determine decendants.

Now I’d like to simply cache the results from this query but I’m getting an error from nHibernate Index was outside the bounds of the array. if I have .SetCacheable(true). If I remove the cache support the query works fine.

The query works fine if I remove the caching support from my call to Session.CreateSQLQuery() Now I’ve looked online to try and find a cause but I haven’t found a consensus amongst the results I found.

So while I am curios why it doesn’t work, I’m more interested in finding a workaround to get caching to work with my CTE in nHibernate?

  • 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-06-15T07:33:39+00:00Added an answer on June 15, 2026 at 7:33 am

    Ended up simply doing the following. I haven’t tested the caching yet but I may have to sacrifices that in favor of the flexibility the CTE provides.

    const string query = "WITH Ancestors(RelationshipId, ParentId, ChildId) " +
                            "AS " +
                            "( SELECT por.RelationshipId, por.ParentId, por.ChildId " +
                            "FROM PseudoObjectRelationships por WHERE ChildId = :objectId " +
                            "UNION ALL " +
                            "SELECT por.RelationshipId, por.ParentId, por.ChildId " +
                            "FROM PseudoObjectRelationships por " +
                            "INNER JOIN Ancestors " +
                            "ON Ancestors.ParentId = por.ChildId " +
                            ") " +
                            "SELECT RelationshipId, ParentId, ChildId FROM Ancestors";
    
    return ((DynamicObjectRepository<TObject>)dynamicObjectRepository).CreateSQLQuery(query)
            .SetGuid("objectId", objectId)
            .SetResultTransformer(Transformers.AliasToBean<Relationship>())
            .List<Relationship>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database holding hierarchical data using the Closure Table method. A
I have a table that's holding metadata for entrys from another table. The metadata-table
I'm working on a project that will have a single table holding lots and
I have table of data that is sorted as follows: Item | Sample |
I have a MySQL table holding lots of records that i want to give
I have a C# Windows app with an SQL table holding voter information. One
I'm querying some data from a table in DB2/z which holds hourly data but
I have a simple entity which is a table holding my user data and
Hello I have table in my database holding some browser information I'm currently using
I have a table in html with some tabular data. The thing is that

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.