NHibernate erstellt einen Proxy über session.Load (), jedoch nicht über die Linq- oder Criteria-API

Ich habe ein seltsames Problem in meinem aktuellen Projekt. Das langsame Laden von Abfragen funktioniert nicht. Wenn ich eine Liste abfrage, ruft nhibernate alle Zuordnungen separat ab.

Ich extrahierte kleine Teile davon und legte sie in eine separate Lösung. Grundsätzlich habe ich jetzt eine Account-Tabelle und eine AccountSync-Tabelle. Beide haben eine ID und eine URL, während die ID nur eine db-guid ist.

Meine Klassen sind:

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; }
}

Wenn ich jetzt ein Objekt über seine Guid lade:

var account = session.Load<HippoAccount>(accountId);
Console.WriteLine(NHibernateUtil.IsPropertyInitialized(account, "Sync"))

... es kehrt zurückfalse und Konto selbst ist ein Proxy.

Aber beim Laden einer Liste über die Kriterien-API:

var account = (HippoAccount)session
    .CreateCriteria(typeof (HippoAccount))
    .Add(Restrictions.Eq("Id", accountId))
    .List()[0];

... die EigenschaftSync wird initialisiert (löst eine zweite Auswahlabfrage aus) und das zurückgegebene Objekt ist kein Proxy.

Ist das Standardverhalten? Was mache ich falsch?

Das Mapping ist:

<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>

Antworten auf die Frage(1)

Ihre Antwort auf die Frage