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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:13:59+00:00 2026-05-13T21:13:59+00:00

these are my entities (simplified): public class IdentificationRequest { private int id; private ICollection<IdentificationRequestStateHistoryItem>

  • 0

these are my entities (simplified):

public class IdentificationRequest
{
    private int id;
        private ICollection<IdentificationRequestStateHistoryItem> stateHistoryItems;

        public virtual string Id
        {
            get { return id; }
            private set { id = value; }
        }                

        public virtual IEnumerable<IdentificationRequestStateHistoryItem> StateHistoryItems
        {
            get { return stateHistoryItems; }
            private set { stateHistoryItems = new List<IdentificationRequestStateHistoryItem>(value); }
        }

        public void ChangeState(IdentificationRequestState state)
        {
        // state transition here
            var stateHistoryItem = new IdentificationRequestStateHistoryItem(state, this);
            stateHistoryItems.Add(stateHistoryItem);
        }     
}


public class IdentificationRequestStateHistoryItem
{
    private int id;
        private IdentificationRequestState state;
        private EntityTimestamp timestamp;

        public virtual string Id
        {
            get { return id; }
            private set { id = value; }
        }                

    public virtual IdentificationRequestState State
    {
        get { return state; }
        private set { state = value; }
    }

    public virtual EntityTimestamp Timestamp
    {
        get { return timestamp; }
        private set { timestamp = value; }
        }
}



public class IdentificationRequestState
{
    // possible states are
    // Received, then id = 10
    // Finished, then id = 20   
    private int id; 

    private string name;

        public virtual string Name
        {
            get { return name; }
            private set { name = value; }
        }

        public virtual string Id
        {
            get { return id; }
            private set { id = value; }
        }        
}

public class EntityTimestamp
    {
        private DateTime createdOn;


        public virtual DateTime CreatedOn
        {
            get { return createdOn; }
            private set { createdOn = value; }
        }

        private IUser createdBy;


        public virtual IUser CreatedBy
        {
            get { return createdBy; }
            private set { createdBy = value; }
        }

        private DateTime changedOn;


        public virtual DateTime ChangedOn
        {
            get { return changedOn; }
            private set { changedOn = value; }
        }

        private IUser changedBy;


        public virtual IUser ChangedBy
        {
            get { return changedBy; }
            private set { changedBy = value; }
        }
}        

So the above reads like the following and is also designed like that in the database.

An IdentificationRequest can track its IdentificationRequestState transitions. So each time a new IdentificationRequestState is assigned, a new IdentificationRequestStateHistoryItem is created and put into a collection of multiple history items. That means, that the current IdentificationRequestState is the last added one (expressed by the createdOn date).

So now my question is, how a query would look like, that lists all IdentificationRequest‘s whos current state is Received, or Finished.

This is what I got so far:

private ICollection<IdentificationRequest> GetByCurrentState(IdentificationRequestState state)
{
    var session = GetCurrentSession();

    var subCriteria = DetachedCriteria.For(typeof(IdentificationRequestStateHistoryItem))            
        .SetProjection(Projections.Property("identificationRequest.Id" ));

    subCriteria.Add(Restrictions.Eq("State", state));

    var criteria = session
        .CreateCriteria(typeof (IdentificationRequest))
        .Add(Subqueries.PropertyIn("Id", subCriteria))
        .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer());

   return criteria.List<IdentificationRequest>();          
}

But when using this query and when search for an identification request with its current state Received, I also get requests with its current state Finished since the request had the state Received before. So I need to do something with Max(CreatedOn) or so…?

NHibernate mappings:

   <class name="IdentificationRequest" table="IdentificationRequest">

    <id name="Id" column="IdentificationRequestId" unsaved-value="-1">
      <generator class="identity"/>
    </id>

    <bag name="stateHistoryItems" access="field" order-by="CreatedOn desc" fetch="join" cascade="all-delete-orphan" lazy="false">
      <key column="IdentificationRequestId"/>
      <one-to-many class="IdentificationRequestStateHistoryItem"/>
    </bag>

  </class>


    <class name="IdentificationRequestStateHistoryItem" table="IdentificationRequestStateHistoryItem">

      <id name="Id" column="IdentificationRequestStateHistoryItemId" unsaved-value="-1">
        <generator class="identity"/>
      </id>

      <many-to-one name="identificationRequest" access="field" lazy="false" cascade="none" column="IdentificationRequestId" class="IdentificationRequest"></many-to-one>

      <many-to-one name="State" lazy="false" fetch="join" cascade="none" column="IdentificationRequestStateId" class="IdentificationRequestState"></many-to-one>

      <component name="Timestamp" class="EntityTimestamp">
        <property name="CreatedOn"/>
        <component name="CreatedBy" class="User">
          <property name="Name" column="CreatedBy" />
        </component>
        <property name="ChangedOn"/>
        <component name="ChangedBy" class="User">
          <property name="Name" column="ChangedBy" />
        </component>
      </component>

    </class>


  <class name="IdentificationRequestState" table="IdentificationRequestState">

    <id name="Id" column="IdentificationRequestStateId" unsaved-value="-1">
      <generator class="assigned"/>
    </id>

    <property name="Name"/>

  </class>    
  • 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-13T21:13:59+00:00Added an answer on May 13, 2026 at 9:13 pm

    You’d save yourself a lot of pain by having a IdentificationRequest.CurrentState property.

    Your thought about max() is correct. You can add another subcriteria where you take the max(date) of all the requeststates for that request, and then add a restriction to your existing subcriteria such that its date = that max date. Gross, but that’s the only way I can think of.

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

Sidebar

Related Questions

No related questions found

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.