2016-05-17 29 views
0

我有一個與Castle ActiveRecord映射類的HQL查詢,並得到以下錯誤:NHibernate.QueryParameterException:無法找到命名參數[參數]。NHibernate與城堡,HQL無法找到參數[參數]

這裏是我的課,簡化:

[Serializable] 
[ActiveRecord("my_table", Schema = "my_schema", UseAutoImport = false, Mutable = false)] 
public class MyTable : MyServerActiveRecord<MyTable> //Extension of ActiveRecordBase<> 
{ 
    [PrimaryKey(PrimaryKeyType.Identity, "my_pk")] 
    public int ID {get;set;} 

    [Property("my_column1")] 
    public int MyColumn1 {get;set;} 

    [Property("my_column2")] 
    public int MyColumn2 {get;set;} 
} 

,並使用SetInt32,而不是SetParameter我的HQL

public static int GetSum(int num1){ 
    IQuery query = session.CreateQuery(@" 
     select sum(case when t.MyColumn2 = 2 then 1 else 0 end)    
     from My.Complete.Namespace.MyTable t 
     where t.MyColumn1 = :num 
     group by t.MyColumn1 
    "); 
    query.SetParameter("num", num1); 
    return query.UniqueResult<Int32>(); 
} 

的方法沒有奏效。我在查詢中驗證了間距。

查詢工作正常,如果我拋出7其中:num是和削減了:num及其設置聲明完全。

+0

我從來沒有見過這樣的麻煩。你可以檢查'query.namedParameters'來看看它在你的'HQL'查詢中找到了什麼嗎? –

+0

@Frédéric,沒有運氣,定義查詢後NamedParameters的長度爲零 – allie

回答

0

所以我想我的填充會話變量是這樣的:

session = ActiveRecordMediator.GetSessionFactoryHolder().GetSessionFactory(typeof(T)).OpenSession(); 

其中T =查詢返回類型,又名Int32

我改變T爲我正在查詢的類,MyTable,並且一切都開始工作。 Bummer Castle/NHibernate不會返回更有幫助的錯誤。 我不好意思在這個問題中不包括會話行。