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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:26:12+00:00 2026-05-14T00:26:12+00:00

Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have

  • 0

Is it possible to perform a global reversed-find on NHibernate-managed objects?

Specifically, I have a persistent class called “Io”. There are a huge number of fields across multiple tables which can potentially contain an object of that type. Is there a way (given a specific instance of an Io object), to retrieve a list of objects (of any type) that actually do reference that specific object? (Bonus points if it can identify which specific fields actually contain the reference, but that’s not critical.)

Since the NHibernate mappings define all the links (and the underlying database has corresponding foreign key links), there ought to be some way to do it.

Imagine this sort of structure:

class Io
{
  public int Id { get; set; }
  // other fields specific to the Io type
}

class ThingOne
{
  public int Id { get; set; }
  public Io SensorInput { get; set; }
  public Io SolenoidOutput { get; set; }
  // other stuff
}

class ThingTwo
{
  public int Id { get; set; }
  public Io SensorInput1 { get; set; }
  public Io SensorInput2 { get; set; }
  public SubThing Doohickey { get; set; }
  // ...
}

class SubThing
{
  public int Id { get; set; }
  public Io ControlOutput1 { get; set; }
  // ...
}

Given a specific instance of Io, I want to discover that it’s referenced by the ThingTwo with id 12. Or that it’s referenced by that and also by the ThingOne with id 16. If possible, also that the first reference is via SensorInput2, for example.

  • 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-14T00:26:12+00:00Added an answer on May 14, 2026 at 12:26 am

    Well the configuration mappings don’t seem to expose the FK relationship so for the time being some reflection can find which object types reference this.
    Note that the code below assumes that you have all nhibernate-mapped classes to a single assembly and also uses C# 3.0 and above for the LINQ support.

    IO toSearch = nhSession.Get<IO>(5);
    var assembly = Assembly.Load("EntityAssembly");
    IList<Type> assemblyTypes = assembly.GetTypes();
    var searchType = toSearch.GetType();
    var typesThatContainedSearchTypeProperty =
        assemblyTypes.Where(
        ast => ast.GetProperties().Count() > 0 &&
        ast.GetProperties().Where(
            astp => astp.PropertyType != null && astp.PropertyType == searchType).Count() > 0);
    

    Now if you also want to get the objects that contain this particular instance of IO you can have a nice MultiCriteria to do it in a single round trip.

    var multiCrit = nhSession.CreateMultiCriteria();
    
    foreach (var type in typesThatContainedSearchTypeProperty)
    {
        //maybe this class has multiple properties of the same Type
        foreach (PropertyInfo pi in type.GetProperties().Where(astp => astp.PropertyType == toSearch.GetType()))
            multiCrit.Add(nhSession.CreateCriteria(type).Add(Restrictions.Eq(pi.Name, toSearch)));
    }
    IList results = multiCrit.List();
    

    as you can guess, since we begin with reflection we can only end with reflection. The results list is an array with each entry the result of each criteria, where each criteria search may be a single result or a list of results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it even possible to perform address (physical, not e-mail) validation? It seems like
Is it possible to perform a named-group match in Perl's regex syntax as with
Is it possible to use a jQuery ajax call to perform Forms Authentication with
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
I have a number of data containers that can send a signal when there
Is it possible to perform a subquery on a QuerySet using another QuerySet? For
I was wondering if it is possible to perform a certain number of operations
In C#, is it possible to perform ToString on a float and get the
What tools are out there that can perform code coverage analysis at the machine
Is is possible to create a custom menu item within eclipse that will perform

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.