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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:19:54+00:00 2026-05-16T20:19:54+00:00

I am looking for a way to build an OR operator into a query

  • 0

I am looking for a way to build an OR operator into a query to look for a particular value in one field of a table as well as in another field of a joined table. This is pretty basic in SQL, but I can’t for the world figure out how to do this in NHibernate. I have been searching the web, but the examples I find are pretty nebulous to me and I find them hard to apply to my particular implementation.

I have an class called Party, with a string-field called reference, which is the main reference. New requirements demanded the option to also being able to add a lot of side-references to a party. So I had to add another class called PartyReference that has a many-to-one relationship to Party.

Now with a given reference I have to look its value up in both this main reference field as well as among the side references. But as long as I cannot figure out to say to NHibernate that EITHER this field must correspond to the value OR one of the others, I cannot make it work.

I have made a workaround that looks like this, but it is inelegant and stupid, as there has to be way to say “OR”:

   public Party GetPartyOnAnyReference(string reference)
       {
           Party party;

           ISession session = Factory.OpenSession();
           ITransaction tx = session.BeginTransaction();
           try
           {
               //first search on main reference
               ICriteria criteria1 = session.CreateCriteria(typeof(Party));
               criteria1.Add(Restrictions.Eq("Reference", reference));
               IList<Party> parties1 = criteria1.List<Party>();
               party = parties1.Count > 0 ? parties1[0] : null;

               //then try with side-references
               if (party == null)
               {
                   ICriteria criteria2 = session.CreateCriteria(typeof(Party));
                   criteria2
                           .SetFetchMode("References", FetchMode.Eager)
                           .CreateCriteria("References")
                           .Add(Expression.Eq("Reference", reference));
                   IList<Party> parties2 = criteria2.List<Party>();
                   party = parties2.Count > 0 ? parties2[0] : null;
               }

               session.Close();
           }
           catch (Exception e)
           {
               tx.Rollback();
               session.Close();

               if (e.GetType().ToString() == "NHibernate.ObjectNotFoundException")
                   party = null;
               else throw;
           }
           return party;
       }

I of course realize I can also solve this issue by simply removing the main reference from the Party class alltogether and treat it on par with the other references, as a PartyReference. But at some stage I will have to use an OR query with NHibernate anyway, so I might just as well solve it now with this particular case.

Any ideas?

  • 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-16T20:19:54+00:00Added an answer on May 16, 2026 at 8:19 pm

    You can use Restrictions.Or or use a Disjunction for multiple or’s.

    session.CreateCriteria<Party>()
        .CreateAlias("References", "r", JoinType.LeftOuterJoin)
        .Add(Restrictions.Or(
            Restrictions.Eq("Reference", reference),
            Restrictions.Eq("r.Reference", reference)))
        .SetResultTransformer(new DistinctRootEntityResultTransformer())
        .List<Party>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to build case statements in a sql select query
I'm looking into a way to build a system that will help our sales
I'm looking for a more efficient way to build this query (double subqueries make
I'm looking a way to build conditional assignments in bash: In Java it looks
I'm looking for a way to concatenate two arbitrary, valid XPath expressions to build
I'm looking the way to build an AddIn for Outlook with Python that add
Q. We're looking for a way to automate build process , run test cases
I'm looking for a way to boost my team's productivity, and one way to
I’m looking for a way to build web site which uses PostreSQL to store
Hope you can help. I'm looking for a way to build an excel sheet

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.