在NHibernate中急切地加载多级子实体会导致重复问题
我有一个模型类,其中包含一些图像和一些功能:
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; }
}
现在我想急切地加载模型的 ModelImages 和功能,我正在使用:
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>();
但结果包含重复的 ModelImage 和 ModelFeatures, 我如何将结果转换器(例如 DistinctRoot)应用于这些子集合?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将查询分为两部分:
第二个查询只是继续填充第一个查询返回的“item”对象的集合,因此不需要使用第二个查询的返回值。
如果您希望在一次往返中执行这两个查询,您可以使用 Future() 而不是 UniqueResult(),然后使用 item.Value 实际执行查询。
I would split the query into two parts:
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.
谢谢 Erik,
NHibernate 中没有标准的方法来做到这一点,这真的很烦人。或者也许我有设计问题?无论如何,我还记得为了避免重复的实体,我们可以在映射中使用 Set 而不是 Bag。
所以我改变了
:
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 :
to