选择具有多个子实体的实体
使用 Active Record/NHibernate,我尝试选择一个具有多个子集合的实体(站点)。
给定 siteId 的 Site 只有一个,但 FindAll 返回该 Site 28 次;由于它正在加载子集合,因此它被重复。如何让它仅加载 1 个站点和子集合的数组?我不介意它是否进行 5 个选择,其中一个用于抓取网站,然后每个子集合进行 1 个选择。
这是代码:
var criteria = DetachedCriteria.For<Site>()
.CreateAlias("TimeZone", "tz", NHibernate.SqlCommand.JoinType.InnerJoin)
.CreateAlias("Logos", "l", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateAlias("SiteColors", "sc", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateAlias("ManagerUsers", "mu", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("Id", siteId));
var sites = FindAll(criteria);
我应该提到的是,我还尝试了使用 fetch 的 hql 方法,但是 fetch 没有获取!它仅返回一个站点(好),但没有任何子集合被急切加载(坏)。
public static Site Get(int id)
{
var query = new SimpleQuery<Site>(@"
from Site s
where s.Id = ?
inner join fetch s.TimeZone tc
left join fetch s.Logos l
left join fetch s.SiteColors sc
left join fetch s.ManagerUsers mu
", id);
var results = query.Execute().ToList();
return results.FirstOrDefault();
}
下面是使用“Future”方法似乎有效的代码:
public static Site Get(int id)
{
var session = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(Site));
var query = session.CreateQuery("from Site s left join fetch s.Logos where s.Id = :id").SetParameter("id", id).Future<Site>();
session.CreateQuery("from Site s left join fetch s.SiteColors where s.Id = :id2").SetParameter("id2", id).Future<Site>();
session.CreateQuery("from Site s left join fetch s.ManagerUsers where s.Id = :id3").SetParameter("id3", id).Future<Site>();
return query.FirstOrDefault();
}
我的所有数据模型类都继承自 SimpleModel 类,我已将 Equals 和 GetHashCode 方法添加到其中:
public abstract class SimpleModel
{
protected SimpleModel()
{
DateCreated = DateTimeAccess.Now;
}
[PrimaryKey]
[DocumentId]
public virtual int Id { get; set; }
[Property]
public virtual DateTime DateCreated { get; set; }
public override bool Equals(object obj)
{
if (obj is SimpleModel)
{
return Id.Equals(((SimpleModel)obj).Id);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
Using Active Record/NHibernate, I'm trying to select an entity (Site) which has multiple child collections.
There is only one Site with the given siteId, yet the FindAll returns the Site 28 times; it's being duplicated due to the child collections that it's loading. How do I get it to load just the 1 Site and arrays of the child collections? I don't mind if it does 5 selects, one to grab the site and then 1 per child collection.
Here is the code:
var criteria = DetachedCriteria.For<Site>()
.CreateAlias("TimeZone", "tz", NHibernate.SqlCommand.JoinType.InnerJoin)
.CreateAlias("Logos", "l", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateAlias("SiteColors", "sc", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateAlias("ManagerUsers", "mu", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("Id", siteId));
var sites = FindAll(criteria);
I should mention that I also tried an hql approach using fetch, however fetch is not fetching! It returns only the one Site (good) but none of the child collections are eagerly loaded (bad).
public static Site Get(int id)
{
var query = new SimpleQuery<Site>(@"
from Site s
where s.Id = ?
inner join fetch s.TimeZone tc
left join fetch s.Logos l
left join fetch s.SiteColors sc
left join fetch s.ManagerUsers mu
", id);
var results = query.Execute().ToList();
return results.FirstOrDefault();
}
Here is code that seems to work, using the "Future" approach:
public static Site Get(int id)
{
var session = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(Site));
var query = session.CreateQuery("from Site s left join fetch s.Logos where s.Id = :id").SetParameter("id", id).Future<Site>();
session.CreateQuery("from Site s left join fetch s.SiteColors where s.Id = :id2").SetParameter("id2", id).Future<Site>();
session.CreateQuery("from Site s left join fetch s.ManagerUsers where s.Id = :id3").SetParameter("id3", id).Future<Site>();
return query.FirstOrDefault();
}
All of my data model classes inherit from a SimpleModel class, which I've added Equals and GetHashCode methods to:
public abstract class SimpleModel
{
protected SimpleModel()
{
DateCreated = DateTimeAccess.Now;
}
[PrimaryKey]
[DocumentId]
public virtual int Id { get; set; }
[Property]
public virtual DateTime DateCreated { get; set; }
public override bool Equals(object obj)
{
if (obj is SimpleModel)
{
return Id.Equals(((SimpleModel)obj).Id);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的条件调用
SetResultTransformer(DistinctRootEntity)
。它将那些重复的Site
实例折叠为单个实例。有关详细信息,请参阅此帖子:http://groups.google.com/group /nhusers/browse_thread/thread/9919812230702ccc
Call
SetResultTransformer(DistinctRootEntity)
on your criteria. It will collapse those duplicateSite
instances to a single instance.For more information, see this thread: http://groups.google.com/group/nhusers/browse_thread/thread/9919812230702ccc