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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:17:05+00:00 2026-05-25T06:17:05+00:00

I have 2 entities (These are broken down to question to be simpler): Entity

  • 0

I have 2 entities (These are broken down to question to be simpler):

Entity A

    public class EntityA
        {
            protected IList<EntityB> _bList = new List<EntityB>();

            virtual public int Id { get; set; }
            virtual public int ExtId { get; set; }


            public virtual void AddB(EntityB b)
            {
                if (!_bList.Contains(b)) _bList.Add(b);
                b.A = this;
                b.ExtId  = this.ExtId;
            }

            public virtual void RemoveB(EntityB b)
            {
                _bList.Remove(b);
            }

            public virtual IList<EntityB> BList
            {
                get { return _bList.ToList().AsReadOnly(); }
            }
        }

Entity A Mapping

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
      <class name="hibernate.domain.mappings.EntityA, hibernate.domain" lazy="true">
        <id name="Id">
          <generator class="native" />
        </id>
        <property type="int" name="ExtId" column="[ExtId]" />
        <bag
          name="BList"
          table="EntityB"
          cascade="all"
          lazy="true"
          inverse="true"
          access="field.camelcase-underscore"
          optimistic-lock="false"
          >
          <key column ="ExtId" property-ref="ExtId" />
          <one-to-many class="hibernate.domain.mappings.EntityB, hibernate.domain" />
        </bag>
    </hibernate-mapping>

Entity B

     public class EntityB
        {
            protected EntityA _a;

            virtual public int Id { get; set; }
            virtual public int ExtId { get; set; }
            virtual public EntityA A
            {
                get { return _a; }
                set { _a = value; }
            }
        }

Entity B Mapping

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="hibernate.domain.mappings.EntityB, hibernate.domain" lazy="true">
    <id name="Id">
      <generator class="native" />
    </id>
    <property type="int" name="ExtId" column="[EXTID]" />
    <many-to-one
            name = "A"
      property-ref ="ExtId"
            not-null="true"
            class = "hibernate.domain.mappings.EntityA, hibernate.domain"
      access="field.camelcase-underscore"
            cascade = "save-update"
            fetch="select"
            insert = "false"
      lazy = "false"
            update = "false"
      column="ExtId"
      />
  </class>
</hibernate-mapping>

What I need to do is to use Queryover to get List of A with paging while selecting the first item of the B associated with A,

I have used following queryover,

    using (ISession session = SessionProvider.OpenSession())
                {
                    var bOver = (QueryOver<EntityB, EntityB>)session.QueryOver(() => bAlias)
                        .JoinAlias(() => bAlias.A, () => aAlias)
                        .SelectList(b => b.Select(() => bAlias.Id))
                        .Take(1);

                    var aOver = session.QueryOver(() => aAlias)
                        .SelectList(l => l.Select(() => aAlias.Id)
                        .SelectSubQuery<EntityB>(bOver));



var result = aOver.Skip(1).Take(1).List<object[]>();
            }

But the generated query is like, following

SELECT TOP (10) y0_,
                (SELECT TOP (10) this_0_.id AS y0_
                 FROM   (SELECT this_.id
                                AS y0_,
                                (SELECT TOP (1) this_0_.id
                                                AS
                                                y0_,
                                                Row_number() OVER(ORDER BY
                                                current_timestamp) AS
                                                __hibernate_sort_row
                                 FROM   entityb this_0_
                                        INNER JOIN entitya aalias1_
                                          ON
this_0_.extid = aalias1_.[EXTID])
AS y1_
FROM   entitya this_) AS QUERY
WHERE  QUERY.__hibernate_sort_row > 1
ORDER  BY QUERY.__hibernate_sort_row)  

And it’s not near correct, So how can I solve this sort of situation (in the real world situation I need to select multiple first items like B with the A)

  • 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-25T06:17:05+00:00Added an answer on May 25, 2026 at 6:17 am

    Ok, I have managed to solved the situation with a little hack to the Nhibernate Projections (Actually adding a custom one)

    [Serializable]
        public class TopRowProjection : SimpleProjection
        {
            private PropertyProjection _projection;
    
            public TopRowProjection(PropertyProjection projection)
            {
                _projection = projection;
            }
    
            public override bool IsAggregate
            {
                get { return true; }
            }
    
            public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
            {
                return _projection.GetTypes(criteria, criteriaQuery);
            }
    
            public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
            {
                SqlStringBuilder result = new SqlStringBuilder().Add(" top(1) ");
                result.Add(_projection.ToSqlString(criteria, position, criteriaQuery, enabledFilters));
                result.Add(" ");
                return result.ToSqlString();
            }
    
            public override string ToString()
            {
                return "select top(1)";
            }
    
            public override bool IsGrouped
            {
                get { return false; }
            }
    
            public override SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
                                                       IDictionary<string, IFilter> enabledFilters)
            {
                throw new InvalidOperationException("not a grouping projection");
            }
        }
    

    And code is modified as follows, and all worked out fine

    using (ISession session = SessionProvider.OpenSession())
                {
                    var bOver = (QueryOver<EntityB, EntityB>)session.QueryOver(() => bAlias)
                        .JoinAlias(() => bAlias.A, () => aAlias)
                        .Select(new TopRowProjection(Projections.Property(() => bAlias.Id)));
    
                    var aOver = session.QueryOver(() => aAlias)
                        .SelectList(l => l.Select(() => aAlias.Id)
                        .SelectSubQuery<EntityB>(bOver));
    
                    var result = aOver.Skip(1).Take(1).List<object[]>();
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two entities. One for Employees: [Table(Name = Employees)] public class Employee
I have entities like these ones: Entity [products(147)] Entity [manufacturer(23)/products(131)] Entity [manufacturer(17)/products(131)] Now, I'm
I have these entities class Foo{ Set<Bar> bars; } class Bar{ Foo parent; String
I've got some entities which have decimal properties on them. These entities' properties are
Say I have entities organized in a hierarchy with Parent being the root entity
I have these entities (this is just an abstraction I created for this post):
I have some entities created with LINQ-to-SQL. Six of these entities (representing values primarily
OK so I have two entities in my data model (let's say entityA and
I have two entities, projects and users. These are modeled in Rails using Mongoid
For example, I have two entities: Employee and Address. Of these enitities, Employee has

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.