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.
Using Criteria API, we can achieve both
leftand/orinnerjoin:LEFT
INNER
leftResult will contain all
Fruitswhile innerResult will contain only these havingColorEDIT: 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 aColor. If this is true, and we won’t ever need these table records without Color selected (having columnColorIdset to NULL) there is a way how to adjust mapping:or in XML
(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…