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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:37:20+00:00 2026-05-13T00:37:20+00:00

Let’s say I have a class Voucher: public class Voucher { public Guid Id

  • 0

Let’s say I have a class Voucher:

public class Voucher
{
    public Guid Id {get;set;}
    public DateTime DateAvailable {get;set;}
}

and a class Entry

public class Entry
{
    public Guid Id {get;set;}
    public Voucher Voucher {get;set;}
    // ... other unrelated properties
}

How can I create an NHibernate Criteria query that finds the first available voucher that is NOT currently assigned to an Entry?

The equivalent SQL would be

select 
    v.Id, v.DateAvailable 
from 
    Voucher v
        left join Entries e on e.VoucherId = v.Id
where 
    v.DateAvailable <= getutcdate() and
    e.Id is null

Edit: I’m still unable to figure this one out. The Voucher table has no reference to the Entries table, but I need to find the first voucher (by date order) that has not been assigned to an entry. This seems like such a simple task, but everything I keep reading about using NHibernate criteria left joins requires the Voucher object to have a property that references the entry. Surely there’s a way to invert the query or add a reference property to the Voucher object without modifying the database to have each table reference the other.

Edit 2: For what it’s worth, I don’t think it’s possible to do what I was trying to do without some modifications. I eventually got a query to work using the Entry as the primary criteria with the Voucher as a sub-criteria, but then UniqueResult returned null, even if the data was there. I guess it just couldn’t make the association.

In case anyone runs into this, I ended up making a foreign key in each table that references the other and using the References<> mapping to associate the two. It’s not idea, because I have to manually set each entity’s sub property to the other to make the association bidirectional, but it at least works without a ton of changes.

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

    Translating your SQL literally:

    var voucher = NHibernateSessionManager.Session.CreateCriteria<Voucher>("v")
                            .Add(Restrictions.Le("v.DateAvailable", DateTime.UtcNow))
                            .CreateCriteria("Entries", "e")
                            .Add(Restrictions.IsNull("e.Id"))
                            .SetMaxResults(1)
                            .UniqueResult<Voucher>();
    

    Now if I understand this correctly there may be an alterantive.
    If the statement: “..finds the first available voucher that is NOT currently assigned to an Entry…” is the same with the statment “finds the first available voucher that has no entries…” (since the voucher is the entity that has many entries… according to your classes) then you could do:

    var voucher = NHibernateSessionManager.Session.CreateCriteria<Voucher>()
                    .Add(Restrictions.IsEmpty("Entries"))
                    .Add(Restrictions.Le("DateAvailable", DateTime.UtcNow))
                    .SetMaxResults(1)
                    .UniqueResult<Voucher>();
    

    …assuming that you have mapped the Entries property in the Voucher entity.

    But maybe I got it wrong…

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

Sidebar

Related Questions

Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say that I have a method with the signature: public static bool ValidDateToSend(DateTime
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's imagine I have a Java class of the type: public class MyClass {
Let's say that I have a set of relations that looks like this: relations
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a user class with columns name and email: Class User
Let's say i have created myTableViewCell class: #import MyTableViewCell.h @implementation MyTableViewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say i have this block of code, <div id=id1> This is some text

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.