mapping - Eager loading an optional one-to-one with NHibernate -
consider following simplified domain:
public class movie { public virtual int id { get; set; } public virtual moviedetail moviedetail { get; set; } } public class moviedetail { public virtual int id { get; set; } public virtual movie movie { get; set; } }
a moviedetail
cannot exist without movie
, movie
exist without moviedetail
(i.e. have no details it).
our database has separate table movie
columns id
, , separate table moviedetail
columns id
, movieid
. there foreign key moviedetail.movieid
movie.id
.
we've got mapped in nhibernate, when getting collection of movie
instances, want left outer join moviedetail
. if not, have n+1 problem when iterating on movie
instances. case now: there separate query every call movie.moviedetail
property.
i've tried one-to-one
mapping, seems case when have both instances. in our case, don't have moviedetail
. also, don't share same primary key.
i've researched formula's, require me make moviedetail
implement iusertype
, putting nhibernate domain. i'd avoid that.
maybe try adding many-to-one relation in movie
mapping moviedetail
, act 1 one mapping.
when set option 'not-null' "false" nullable suppose. don't know if lazy loading or not, when moviedetail
is loaded when needed , not left join construction.
shouldn't properties virtual in both classes?
<many-to-one name="moviedetail" column="id" class="moviedetail" not-null="false" lazy="false"/>
Comments
Post a Comment