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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:36:18+00:00 2026-06-13T05:36:18+00:00

I have the following domain model public interface IAppliedTo { public Guid Id {

  • 0

I have the following domain model

public interface IAppliedTo
{
    public Guid Id { get; set; }
}

public class Widget
{
    public DateTime DateCreated { get; set; }
    public Guid Id { get; set; }
    public ISet<IAppliedTo> AppliesTo { get; set; }
}


public class Master : IAppliedTo
{
    public Guid Id { get; set; }
}

Widget mapping has AppliesTo defined as:

<set name="AppliesTo" table="WidgetAppliesTo" lazy="true" >
    <key column="WidgetId"></key>
    <many-to-any  id-type="Guid" meta-type="int" >
      <meta-value  class="Master" value="1"/>

      <column name="ObjectType" />
      <column name="ObjectId" />

    </many-to-any>
  </set>

I am writing a query using QueryOver on Master, and I want to pull out the last CreatedDate for any Widget that applies to the Master.

Essentially I want something like:

            Master mast=null;
            Widget widg=null;
            var widgetQuery=QueryOver.Of<Widget>(()=>widg);

            var query = NHibernateSessionManager.Instance.GetSessionFrom()
                .QueryOver<Master>(() => mast)
                .Where(() => mast.Name == "Josh")
                .SelectList(l => 
                    l.SelectSubQuery(widgetQuery.Where(() => widg.AppliesTo.Contains(mast))
                        .Select(Projections.Max(() => widg.DateCreated))
                    )
                 );

If my AppliesTo was a specific type I’d know how to do this, but how can I tell nHibernate to use ObjectId. Essentially I am trying to get this query:

SELECT *,(SELECT MAX(DateCreated) 
          FROM Widget 
          INNER JOIN WidgetAppliesTo
            on Widget.Id=WidgetAppliesTo.ActivityId
            WHERE objectId=[master].Id)
FROM [Master]
  • 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-13T05:36:20+00:00Added an answer on June 13, 2026 at 5:36 am

    So far the best I’ve managed to do is to use a Sql Projection to add this in.

        .Select(Projections.SqlProjection(
        "(select max(datecreated) from Widget wid
         inner join WidgetAppliesTo widTo on wid.Id=widTo.ActivityId and widTo.ObjectId=this_.Id)
     as lastTouchDate",
    new string[]{"lastTouchDate"},
    new NHibernate.Type.IType[]{NHibernateUtil.DateTime})
    .WithAlias(() => jobDto.LastTouchDate));
    

    While this works I’d love to see a non-hacked solution

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

Sidebar

Related Questions

Given domain model... public class Entity { public int Id { get; set; }
I have the following domain model: public class Name { private readonly string fullName;
I have the following domain model setup using EF code first: public class User
I have the following domain model: public class Campaign { public virtual long Id
I have the following domain objects: public class UserSpecialization : PersonSpecialization, IUserSpecialization { public
I have the following domain: public class FileInformation { public String FileName; public String
I have a domain data model which returns a class such as the following:
We have the following Domain objects :- public class UserDevice : BaseObject { //
I have the following listview : http://www.vogella.de/articles/AndroidListView/article.html the chaper: 8. Tutorial: Domain Model and
I have the following Grails domain class: class Product { String name Float basePrice

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.