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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:21:29+00:00 2026-05-23T05:21:29+00:00

Using nHibernate 2. In our system we have a table that stores records which

  • 0

Using nHibernate 2.

In our system we have a table that stores records which may point to any of the other tables in the system, as well as some additional fields. e.g.

class PointerTable
{
    public int ID;
    public string ObjectName;
    public int RecordID;
    ... additional fields
}

The ObjectName and RecordID fields point to the name of another table in the system, and the ID of the record in that table that this record points to. There are no relationships between this table and the other tables in the system.

I need to be able to join to this table when I retrieve records from the other tables in the system, as I need to determine if a table has any records in PointerTable that point to it. i.e. get the count of the records in the PointerTable for this record.

I’ve tried to do this in HQL:

select a.ID, e.Field1, a.Field2, a.Field3,
            count(select * 
             from PointerTable p
             where p.ObjectName = "Table1"
               and p.RecordID = a.ID)
from Table1 a 
where a.ParentTable.ID = :ID

I’ve also looked into putting a mapping into the XML configuration files for all the entities in the system which will be pointed to by this table. So I could have a collection of PointerTable records in each of my other entities. Though I’m not sure if this is possible. I can’t find how to set the mapping to map on 2 fields rather than the primary key.

Something like:

<bag name="PointerRecords" table="PointerTable" lazy="true" inverse="true">      
     <key>
         <column name="ThisEntityID" />
         <column name="ObjectName" />  ?? Hard coded
    </key>
    <one-to-many class="PointerTable" not-found="ignore"/>      
</bag>

Is this even possible?

Basically all I’m trying to do is the following SQL query. But ideally we’d like to do this through HQL or mappings.

SQL

select a.ID, e.Field1, a.Field2, a.Field3, Count(d.ID)
from Table1 a 
inner join Table2 e on a.ParentID=e.ID
left outer join PointerTable d on d.ObjectName = 'Table1' and d.RecordID = a.ID
where c.ID = @ID
group by a.ID, e.Field1, a.Field2, a.Field3
  • 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-23T05:21:30+00:00Added an answer on May 23, 2026 at 5:21 am

    the idea here would be to map this collection with a where clause

    using fluentmapping:

    public SomeEntityMap(){
        Table("SomeTable");
    
        HasMany(x => x.PointerRecords)
            .Table("PointerTable")
            .KeyColumn("ThisEntityID")
            .Where("ObjectName = 'SomeTable'")
    }
    

    Begin Edit

    using xml-mapping

    <bag name="PointerRecords" table="PointerTable" lazy="true" inverse="true" where ="ObjectName = 'SomeTable'">
        <key column="ThisEntityID"/>
        <one-to-many class="PointerTable" not-found="ignore"/>
    </bag>
    

    End Edit

    and query like

    object[] results = session.CreateCriteria<SomeEntity>()
        .Add(Restrictions.Eq("Id", id))
        .SetProjection(Projections.ProjectionList()
            .Add(Projections.Property("Field1"))
            ...
            .Add(Projections.Count("PointerRecords")));
        .List()
    

    or

    var entities = session.CreateCriteria<SomeEntity>()
        .Add(Restrictions.Eq("Id", id)
        .SetFetchMode("PointerRecords", NHibernate.FetchMode.Eager)
        .List<SomeEntity>();
    
    entities[0].PointerRecords.Count
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using Nhibernate as our data access layer. We have a table of
We have a fairly robust system using NHibernate, we're in the middle of migrating
We have implemented our domain model using the accountability pattern, which we are trying
We have a Fluent NHibernate mapping test that is passing on our local machines,
I have 2 classes that reference each other. It's a weird situation that our
We are attepting to profile our NHibernate application using NHProf. We have a series
I am using Unity with MVC and NHibernate. Unfortunately, our UnitOfWork resides in a
Using NHibernate from C# and only HQL (not SQL) in a way that is
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
I'm using NHibernate to connect to an ERP database on our DB2 server. We

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.