简体   繁体   中英

loading multi-level child entities eagerly in NHibernate cause duplication problem

i have a Model class which contains some images and some features :

public class Model
{
    public int ModelId { get; set; }
    public string ModelName { get; set; }
    public virtual IList<Feature> ModelFeatures { get; set; }
    public virtual IList<ModelImage> ModelImages { get; set; }
}

public class ModelImage
{
    public virtual int ModelImageId { get; set; }
    public virtual Model Model { get; set; }
    public virtual Resource Image { get; set; }
    public virtual int DisplayOrder { get; set; }
}

public class Feature
{
    public virtual int FeatureId { get; set; }
    public virtual string Title { get; set; }
    public virtual string Text { get; set; }
}

now i want to load ModelImages and Features of a Model eagerly, i'm using :

item = session.CreateCriteria<Model>()
       .Add(NHibernate.Criterion.Expression.Where<Model>(o => o.ModelId == id))
       .SetFetchMode("ModelImages", NHibernate.FetchMode.Eager)
       .SetFetchMode("ModelImages.Image", NHibernate.FetchMode.Eager)
       .SetFetchMode("ModelFeatures", NHibernate.FetchMode.Eager)
       .SetResultTransformer(NHibernate.Transform.Transformers.DistinctRootEntity)
       .UniqueResult<Model>();

but the result contain duplicate ModelImage and ModelFeatures, how could i apply a result transformer such as DistinctRoot to these child collections ?

Thanks

I would split the query into two parts:

item = session.CreateCriteria<Model>()
   .Add(NHibernate.Criterion.Expression.Where<Model>(o => o.ModelId == id))
   .SetFetchMode("ModelFeatures", NHibernate.FetchMode.Eager)
   .UniqueResult<Model>();

session.CreateCriteria<Model>()
   .Add(NHibernate.Criterion.Expression.Where<Model>(o => o.ModelId == id))
   .SetFetchMode("ModelImages", NHibernate.FetchMode.Eager)
   .SetFetchMode("ModelImages.Image", NHibernate.FetchMode.Eager)
   .UniqueResult<Model>();

The second query just continues to populate the collections of the "item" object returned from the first query, so there is no need to use the return value from the second query.

If you want those two queries to be executed in a single round trip you can use Future() instead of UniqueResult() and then use item.Value to actually execute the queries.

Thank you Erik,

it's really annoying that there is not a standard way to do so in NHibernate. or maybe i have a design problem ? anyway i also remembered that to avoid duplicate entities we could use a Set instead of using a Bag in our mapping.

so i changed :

<bag name="ModelImages" table="ModelImages" cascade="all-delete-orphan" inverse="true">
  <key column="ModelId"/>
  <one-to-many class="ModelImage"/>
</bag>

to

<set name="ModelImages" table="ModelImages" cascade="all-delete-orphan" inverse="true">
  <key column="ModelId"/>
  <one-to-many class="ModelImage"/>
</set>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM