2010-04-22 117 views
0

我對我的過濾器使用了這個hql查詢。查詢完美工作,除了寬度(字符串)部分。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和大小都相同的映射關係和。

這是錯誤;

NHibernate.QueryException:鄰近收集非法 語法:尺寸[選擇從.Domain.ColorGroup 不同C C 內部聯接c.Products p其中 p.ShowOnline = 1和p.ProductType.ID = 1和p.FinishGroup.ID = 5和p.Size.Width = 4]

任何想法?

編輯:

順便說一下在這個項目中我用這個LINQ查詢這實在是simmilar HQL之一。因此,我不認爲這是一個misstype或者更根本的錯誤..

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

如果刪除寬度條件,查詢是否運行,即不會引發錯誤? – 2010-04-22 08:46:28

+0

是的,它正在處理它。 – ygit 2010-04-22 08:48:31

回答

0

如果Width是一個字符串:

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

但我建議你use parameters在您的查詢,而不是字符串連接。

+0

我試過了,但沒有奏效。同樣的錯誤。 – ygit 2010-04-22 08:14:20

0

看起來像產品屬性「大小」不存在。它是「大小」還是「ProductSize」或類似的東西?如果刪除該條件,查詢是否工作?

+0

它的大小和存在。我更新了問題。 – ygit 2010-04-22 08:33:06

0

我猜你需要在查詢中顯式加入Size實體,因爲其他兩個表(ProductType,FinishGroup)使用它們的主鍵進行比較我猜測也許這就是它的原因? (因爲它們可能是「屬於」關係,意味着它們的ID實際上在Product-Table中)。

+0

謝謝你的回答。我嘗試了這樣的建議,但不幸的是錯誤是一樣的; =「從ColorGroup c內連接c中選擇不同的c。產品p內連接p.Size s」 if(width!=「」) queryStr + =「and s.Width =」+ width; – ygit 2010-04-22 09:27:50