NHibernate tworzy proxy poprzez session.Load (), ale nie poprzez API Linq lub Criteria
Mam dziwny problem w moim obecnym projekcie. Leniwe ładowanie zapytań nie działa. Kiedy sprawdzam listę, nhibernate pobiera osobno wszystkie skojarzenia.
Wyodrębniłem małe części i umieściłem w osobnym roztworze. Zasadniczo to, co mam teraz, jest tabelą kont i tabelą synchronizacji kont. Oba mają identyfikator i adres URL, podczas gdy identyfikator to tylko db-guid.
Moje klasy są:
public class HippoAccount
{
public virtual Guid Id { get; set; }
public virtual string Url { get; set; }
public virtual HippoAccountSync Sync { get; set; }
}
public class HippoAccountSync
{
public virtual Guid Id { get; set; }
public virtual string Url { get; set; }
public virtual HippoAccount Account { get; set; }
}
Kiedy teraz ładuję obiekt przez jego guid:
var account = session.Load<HippoAccount>(accountId);
Console.WriteLine(NHibernateUtil.IsPropertyInitialized(account, "Sync"))
... wracafalse
a samo konto jest proxy.
Ale podczas ładowania listy za pomocą interfejsu API kryteriów:
var account = (HippoAccount)session
.CreateCriteria(typeof (HippoAccount))
.Add(Restrictions.Eq("Id", accountId))
.List()[0];
... własnośćSync
zostaje zainicjowany (odpalenie drugiego zapytania wyboru), a zwrócony obiekt nie jest proxy.
Czy to domyślne zachowanie? Co się mylę?
Mapowanie to:
<class name="HippoAccount" table="AllAccounts">
<id name="Id" type="guid">
<generator class="guid"/>
</id>
<property name="Url" />
<many-to-one
class="HippoAccountSync"
name="Sync"
not-found="ignore"
property-ref="Url">
<column name="url" />
</many-to-one>
</class>
<class name="HippoAccountSync"
mutable="false"
table="Accounts">
<id name="Id" type="guid">
<generator class="guid"/>
</id>
<property name="Url">
<column name="serviceUri" />
</property>
<many-to-one class="HippoAccount"
name="Account"
property-ref="Url"
not-found="ignore">
<column name="serviceUri" />
</many-to-one>
</class>