【问题标题】:HQL query problemHQL查询问题
【发布时间】:2011-02-10 23:02:56
【问题描述】:

我将这个 hql 查询用于我的过滤器。查询除了宽度(字符串)部分外工作正常。

这是查询,

public IList<ColorGroup> GetDistinctColorGroups(int typeID, int finishID, string width)
            {
                string queryStr = "Select distinct c from ColorGroup c inner join c.Products p " +
                                  "where p.ShowOnline = 1 ";


                if (typeID > 0)
                    queryStr += " and p.ProductType.ID = " + typeID;

                if (finishID > 0)
                    queryStr += " and p.FinishGroup.ID = " + finishID;

                if (width != "")
                    queryStr += " and p.Size.Width = " + width;

                IList<ColorGroup> colors = NHibernateSession.CreateQuery(queryStr).List<ColorGroup>();

                return colors;
            }

ProductType 和 Size 具有相同的映射和关系。

这是错误;

NHibernate.QueryException:非法 集合附近的语法:Size [Select 与 .Domain.ColorGroup c 不同的 c 内连接 c.Products p where p.ShowOnline = 1 和 p.ProductType.ID = 1 和 p.FinishGroup.ID = 5 和 p.Size.Width = 4]

有什么想法吗?

编辑:

顺便说一句,在这个项目中,我使用了这个 linq 查询,它与 hql 非常相似。所以我不认为这是一个错误类型或更根本的错误..

colorOfSerie = (from p in products where p.Size.Width.Equals(width) select p.ColorGroup).Distinct().ToList<ColorGroup>();

【问题讨论】:

  • 如果删除宽度条件,查询是否运行,即不会引发错误?
  • 是的,没有它也可以工作。

标签: c# nhibernate hql


【解决方案1】:

如果Width 是一个字符串:

queryStr += " and p.Size.Width = '" + width + "'";

但我建议您在查询中使用use parameters 而不是字符串连接。

【讨论】:

    【解决方案2】:

    产品属性“尺寸”似乎不存在。它是“size”还是“ProductSize”或类似的东西?如果删除该条件,查询是否有效?

    【讨论】:

      【解决方案3】:

      我猜您需要在查询中显式地加入 Size 实体,因为其他两个表(ProductType、FinishGroup)是使用它们的主键进行比较的,我猜这也许就是它起作用的原因? (因为它们可能是“属于”关系,这意味着它们的 ID 实际上在 Product 表中)。

      【讨论】:

      • 感谢您的回答。我试过你的建议,但不幸的是错误是一样的; = "Select distinct c from ColorGroup c inner join c.Products p inner join p.Size s " if (width != "") queryStr += " and s.Width = " + width;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      • 2014-07-18
      相关资源
      最近更新 更多