2

I am using FluentNHibernate and have two classes that have a 1:1 relationship. ItemA includes

public virtual ItemB { get; set; }

and mapped with

   HasOne(x => x.ItemB).ForeignKey("ItemID");

I am only interested in ItemA, but because it has the following map I'm seeing two database calls when I run:

 var res = session.Query<ItemA>()
                  .SingleOrDefault(x => x.ItemId.ToString() =="AC3E30FF-E767-440F-AB1F-0000293C5A0C");

I get a select for ItemA table and a select for ItemB table.

If I then make a change back to References, I get only one:

References(x => x.ItemB).ForeignKey("ItemId");

This is perhaps trivial, but I have another class which has been given a number of "HasOne" relationships for tables I do not need to be using and yet Select queries are becoming rather complex.

I tried adding "Lazyload" to the mapping, but that didn't seem to change anything. Is there any way of keeping the HasOne relationship but being able to generate a simple, single "select" query for my "Query" (i.e. Select [columns] from ItemA where ItemID =...)?

Dave Alger
  • 339
  • 7
  • 23

1 Answers1

2

No, there is no way how to achieve that. One-to-one will simply be eager, no lazy setting will help.

But as stated here: 5.1.11. one-to-one

A one-to-one association ...

... Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:

<many-to-one name="Person" class="Person" column="PERSON_ID" unique="true"/>`
// which is in fluent References()

And this association may be made bidirectional by adding the following to the Person mapping:

<one-to-one name="Employee" class="Employee" property-ref="Person"/>

So, I would suggest go with References mapping

References(x => x.ItemB).Unique().Cascade.All();

See: How to do a fluent nhibernate one to one mapping?

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335