Domanda

Ho un'entità con una proprietà che fa riferimento ad altra entità (riferimento in esempi).

Con HQL posso farlo:

select e.ReferenceEntity from Entity e where e.Id = :entityId

Nhibernate mi darà l'istanza di riferimento senza pigro.

Con query over sto provando a farlo:

Session.QueryOver<Entity>()
.Where(e => e.Id == entityId)
.Select(e => e.ReferenceEntity)
.SingleOrDefault<ReferenceEntity>()

Con Queryover Nhibernate mi sta dando la riferimento ma pigro.

Voglio ottenere riferimento con il caricamento desideroso usando queryover come faccio con HQL.

Grazie

È stato utile?

Soluzione

Suggerimento n. 1

Potresti fare un po 'di manipolazione LINQ dopo aver eseguito la query per prendere i dati desiderati.

var result = Session.QueryOver<Entity>()
    .Where(e => e.Id == entityId)        // Filter,
    .Fetch(e => e.ReferenceEntity).Eager // join the desired data into the query,
    .List()                              // execute database query,
    .Select(e => e.ReferenceEntity)      // then grab the desired data in-memory with LINQ.
    .SingleOrDefault();
Console.WriteLine("Name = " + result.Name);

È semplice e fa il lavoro.

Nel mio test, ha provocato una singola query. Ecco l'output:

SELECT
    this_.Id as Id0_1_, this_.Name as Name0_1_, this_.ReferenceEntity_id as Referenc3_0_1_,
    q5379349_r2_.Id as Id1_0_, q5379349_r2_.Name as Name1_0_
FROM
    [Entity] this_
    left outer join [ReferenceEntity] q5379349_r2_
        on this_.ReferenceEntity_id=q5379349_r2_.Id
WHERE this_.Id = @p0;

Suggerimento n. 2

Un altro approccio sarebbe quello di usare una sottoquery esiste, che sarebbe leggermente più complessa, ma restituirebbe il risultato giusto la prima volta senza alcuna necessità di manipolazione post-database:

ReferenceEntity alias = null;
var result = Session.QueryOver(() => alias)
    .WithSubquery.WhereExists(QueryOver.Of<Entity>()
        .Where(e => e.Id == entityId)                 // Filtered,
        .Where(e => e.ReferenceEntity.Id == alias.Id) // correlated,
        .Select(e => e.Id))                           // and projected (EXISTS requires a projection).
    .SingleOrDefault();
Console.WriteLine("Name = " + result.Name);

Testato - Risultati in una singola query:

SELECT this_.Id as Id1_0_, this_.Name as Name1_0_
FROM [ReferenceEntity] this_
WHERE exists (
    SELECT this_0_.Id as y0_
    FROM [Entity] this_0_
    WHERE this_0_.Id = @p0 and this_0_.ReferenceEntity_id = this_.Id);

Altri suggerimenti

Se ti ho capito correttamente, questo è ciò di cui hai bisogno:

Session.QueryOver<Entity>()
 .Where(e => e.Id == entityId)
 //!!!
 .Fetch(e=>e.ReferenceEntity).Eager
 .Select(e => e.ReferenceEntity)
 .SingleOrDefault<ReferenceEntity>()

Prova questo:

Session.QueryOver<Entity>()
 .Where(e => e.Id == entityId)
 .Fetch(e=>e.ReferenceEntity).Eager
 .Select(e => e.ReferenceEntity)
 .TransformUsing(Transformers.AliasToBean<ReferenceEntity>())
 .SingleOrDefault<ReferenceEntity>()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top