用于获取 NHibernate 属性的子查询
我正在使用 NHibernate 作为 Web 应用程序。我有一个这样的模型:
public class ProductViewModel {
public virtual int Id { get; set; }
/* others simple properties */
public virtual IList<OfferViewModel> LastOffers { get; set; }
public ProductViewModel() { }
}
public class OfferViewModel {
public virtual string UserName { get; set; }
public virtual decimal Prince { get; set; }
public OfferViewModel() { }
}
我想知道如何使用 HQL 子查询获取“LastOffers”集合属性?我想获取最新报价前 10 名的信息。我的模型已正确映射,但我不知道执行子查询获取此属性的语法。
今天,我使用这样的命令来获取我的 ViewModel:
public IList<ProductViewModel> GetProductsForSalles()
{
return
Session.CreateQuery(@"select p.Id as Id,
p.Name as Name,
p.Price as Price,
p.Price as Date
/* FETCH LastOffers? */
from Product p
where p.Active=true and (p.Status=:Status)
order by a.Date asc")
.SetParameter("Status", Status.Started)
.SetMaxResults(50)
.SetResultTransformer(Transformers.AliasToBean<ProductViewModel>())
.List<ProductViewModel>();
}
谢谢!
I'm using NHibernate for a web application. I have a model like this:
public class ProductViewModel {
public virtual int Id { get; set; }
/* others simple properties */
public virtual IList<OfferViewModel> LastOffers { get; set; }
public ProductViewModel() { }
}
public class OfferViewModel {
public virtual string UserName { get; set; }
public virtual decimal Prince { get; set; }
public OfferViewModel() { }
}
I would like to know, how to fetch the "LastOffers" collection property with a HQL subquery ? I want to fetch it with top 10 last offers. I have my model mapped correctly but I don't know the sintax to do a subquery fetch this property.
Today, I'm using a command like this to fetch my ViewModel:
public IList<ProductViewModel> GetProductsForSalles()
{
return
Session.CreateQuery(@"select p.Id as Id,
p.Name as Name,
p.Price as Price,
p.Price as Date
/* FETCH LastOffers? */
from Product p
where p.Active=true and (p.Status=:Status)
order by a.Date asc")
.SetParameter("Status", Status.Started)
.SetMaxResults(50)
.SetResultTransformer(Transformers.AliasToBean<ProductViewModel>())
.List<ProductViewModel>();
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您提供的描述,我猜您需要加载一组预加载的最后报价的产品,并加入 FK 的产品中。下面的代码应该这样做:
不是睾丸,但想法是我们连接两个表,结果将“折叠”到产品的每一行(通过
DistinctRootEntityResultTransformer
)抱歉,该示例不在 HQL 中- 我更喜欢 Criterias 和 QueryOver<>因为更稳定。
Based on description you provided I guess you need to load a set of products with preloaded lastoffers, joined to products by FK. The code below should do this:
Not testes, but idea is that we join two tables and the result will be 'collapsed' to each row of product (by
DistinctRootEntityResultTransformer
)Sorry that the example is not in HQL - I prefer Criterias and QueryOver<> as more stable.