NHibernate - Paging e le entità eager loading
-
30-09-2019 - |
Domanda
Ho due entità, una squadra e un dipendente.
Voglio ottenere un elenco di dipendenti con squadre caricati desiderosi. L'elenco è da paging.
public PagedList<Employee> GetAllEmployeesWithEagerLoadedTeams(int page, int pageSize)
{
var criteria = GetSession()
.CreateCriteria(typeof (Employee))
.SetFetchMode(DomainModelHelper.GetAssociationEntityNameAsPlural(typeof (Team)),FetchMode.Eager);
var totalCount = criteria
.SetProjection(Projections.RowCount())
.FutureValue<Int32>().Value;
return criteria
.SetFirstResult(page * pageSize)
.SetMaxResults(pageSize)
.Future<Employee>()
.ToPagedList(page, pageSize, totalCount);
}
Perché questo non funziona?
risultati SQL:
SELECT count(*) as y0_ FROM [Employee] this_;
SELECT TOP 10 y0_ FROM (SELECT count(*) as y0_,
ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row FROM [Employee] this_) as query WHERE query.__hibernate_sort_row > 10 ORDER BY query.__hibernate_sort_row;
Soluzione
Si sta tentando di riutilizzare la variabile criteri che ha già la proiezione conteggio e valore futuro specificato. È necessario creare un nuovo criterio per la seconda query.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow