用于获取 NHibernate 属性的子查询

发布于 2024-10-05 14:58:56 字数 1465 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2024-10-12 14:58:56

根据您提供的描述,我猜您需要加载一组预加载的最后报价的产品,并加入 FK 的产品中。下面的代码应该这样做:

return Session.CreateCriteria(typeof(ProductViewModel), "p")
  .CreateCriteria("p.LastOffers", "lastoffers")
  .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())
  .Add(Restrictions.Eq("p.Active", true))
  .Add(Restrictions.Eq("p.Status", Status.Started))
  .SetMaxResults(50)
  .List<ProductViewModel>();

不是睾丸,但想法是我们连接两个表,结果将“折叠”到产品的每一行(通过 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:

return Session.CreateCriteria(typeof(ProductViewModel), "p")
  .CreateCriteria("p.LastOffers", "lastoffers")
  .SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())
  .Add(Restrictions.Eq("p.Active", true))
  .Add(Restrictions.Eq("p.Status", Status.Started))
  .SetMaxResults(50)
  .List<ProductViewModel>();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文