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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:58:48+00:00 2026-05-15T09:58:48+00:00

(Felt quite helpless in formulating an appropriate title…) Is it possible to define a

  • 0

(Felt quite helpless in formulating an appropriate title…)

Is it possible to define a mapping for a class such that some properties are fetched from another table using a join query?

In my C# app I display a list of “A” objects, along with some properties of their associated “B” objects and properties of B’s associated “C” objects:

A.Name    B.Name    B.SomeValue    C.Name
Foo       Bar       123            HelloWorld
Bar       Hello     432            World
...

To clarify: A has an FK to B, B has an FK to C. (Such as, e.g. BankAccount -> Person -> Company).

If I extend class A by properties “BName”, “BSomeValue” and “CName”, can I make [N]Hibernate issue a query like

select A.Name, B.Name as BName, B.SomeValue as BSomeValue, C.Name as CName
from A
join B on A.B_ID = B.ID
join C on B.C_ID = C.ID

and map all of the results of these query onto A’s properties?


Background:

I have tried two approaches to load these properties from the database (using NHibernate): A fast approach and a clean approach. My eventual question is how to do a fast & clean approach.

Fast approach:

  • Define a view in the database which joins A, B, C and provides all these fields.
  • In the A class, define properties “BName”, “BSomeValue”, “CName”
  • Define a hibernate mapping between A and the View, whereas the needed B and C properties are mapped with update="false" insert="false" and do actually stem from B and C tables, but Hibernate is not aware of that since it uses the view.

This way, the listing only loads one object per “A” record, which is quite fast. If the code tries to access the actual associated property, “A.B”, I issue another HQL query to get B, set the property and update the faked BName and BSomeValue properties as well.

Clean approach:

  • There is no view.
  • Class A is mapped to table A, B to B, C to C.
  • When loading the list of A, I do a double left-join-fetch to get B and C as well:
    from A a left join fetch a.B left join fetch a.B.C
  • B.Name, B.SomeValue and C.Name are accessed through the eagerly loaded associations.

The disadvantage of this approach is that it gets slower and takes more memory, since it needs to created and map 3 objects per “A” record: An A, B, and C object each.

Fast and clean approach:

I feel somehow uncomfortable using a database view that hides a join and treat that in NHibernate as if it was a table. So I would like to do something like:

  • Have no views in the database.
  • Declare properties “BName”, “BSomeValue”, “CName” in class “A”.
  • Define the mapping for A such that NHibernate fetches A and these properties together using a join SQL query as a database view would do.
  • The mapping should still allow for defining lazy many-to-one associations for getting A.B.C

My questions:

  • Is this possible?
  • Is it [un]artful?
  • Is there a better way?
  • 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-15T09:58:49+00:00Added an answer on May 15, 2026 at 9:58 am

    Your “clean” approach is closest to how it should be (the others aren’t even worth trying)

    Before going the join fetch way, you should see what happens without it. Depending on the usage, you might find that’s good enough.

    You might also want to read about batch-size and caching in the docs.

    And, above all, you shouldn’t try to microoptimize before finding actual problems. When you say “The disadvantage of this approach is that it gets slower and takes more memory”, I don’t think you’ve actually measured it.

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

Sidebar

Related Questions

I've been reading this question and felt that I don't quite agree with the
I have always felt that in general the main work of a class should
I'm coding a Vector class in C# and felt that indexers would be a
Case Quite often I find myself staring at some old code that doesn't look
I've always felt that my graphic design skills have lacked, but I do have
I always felt that expecting exceptions to be thrown on a regular basis and
Before I jump headlong into C#... I've always felt that C, or maybe C++,
When I started to write the first SQL-Statements in my programs I felt quite
Currently I have 4 solutions that are independent projects, however there is quite a
Since the Date class in Java was deprecated, I had quite less frequently been

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.