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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:23:18+00:00 2026-06-15T02:23:18+00:00

I have a many-to-one relation mapped between two entities A and B. I will

  • 0

I have a many-to-one relation mapped between two entities A and B. I will use Fruit and Color to simulate my scenario:

Assuming all the fruit has only one color. So I have a Color property in my Fruit class and in my mapping code for Fruit I have:

ManyToOne(f=>f.Color, mapper=>mapper.Column("ColorId"));

And the generated SQL has the following:

Select f0_.Name, f0...From Fruit f0_
  left outer join Color c0_
  on f0_.ColorId = c0_.id

I am wondering if there is any way for force an inner join instead of outer join. Because from the business perspective, a fruit without a color is not really a fruit and shouldn’t exist.

  • 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-15T02:23:19+00:00Added an answer on June 15, 2026 at 2:23 am

    Using Criteria API, we can achieve both left and/or inner join:

    LEFT

    // default left outer join
    var leftResult = session.CreateCriteria<Fruit>()
      .SetFetchMode("Color", NHibernate.FetchMode.Join)
      .List<Fruit>();
    

    INNER

    // explicit inner join
    var innerResult = session.CreateCriteria<Fruit>()
      .CreateCriteria("Color", NHibernate.SqlCommand.JoinType.InnerJoin)
      .List<Fruit>();
    

    leftResult will contain all Fruits while innerResult will contain only these having Color

    EDIT: specific solution applied inside mapping

    Well, the default left join cannot be changed: Inner or Right Outer Join in Nhibernate and Fluent Nhibernate on Many to Many collection. But what about using other NHibernate feature:

    Let’s say that our Fruit, is only meaningful, if there is a Color. If this is true, and we won’t ever need these table records without Color selected (having column ColorId set to NULL) there is a way how to adjust mapping:

    // Mapping Fruit
    Table("Fruits");
    Where("colorId IS NOT NULL");
    ...
    

    or in XML

      <class name="Fruit" table="Fruits" where="colorId IS NOT NULL" ... >
    

    (see http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-class)

    So this, won’t change LEFT to INNER, but at least the mapping will allow to have only colorful fruits…

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

Sidebar

Related Questions

i have a relation (one to many/ many to one) between parent and child
I have a many to many relation between two tables in my database (MySql)
I have a simple One-to-Many relation mapped with SqlAlchemy: Base = declarative_base() class Type(Base):
Lets say I have two tables - child and parent with many-to-one relation. What
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
i have an entity Entity1 that have one to many relation with Entity2 as
I have a question about Criteria method, one-to-many relation to the database, 'one' is
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
I have a one-many relation: Object ReportCategory: Table("RCA_REPORT_CATEGORY"); Id(r => r.Id, "RCA_ID"); Map(r =>
I have a number of many to many relationships between two tables with conditionals

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.