2014-10-29 84 views
0

我是新與NHibernate與HQL,但我試圖執行一個HQL判決,但我得到了以下錯誤簡單的HQL句子無法執行查詢

An exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll but was not handled in user code 

Additional information: Could not execute query[SQL: SQL not available] 

誰能幫我找到我哪裏做了一些錯誤?

CODE

var valoresAgregadosAnoAtual2 = _repositorio.GetReceitas2<ReceitaGeral>(
    "SELECT MONTH(p.DateHourDataCreation), SUM(p.Value) " + 
    "FROM Receita p " + 
    "WHERE YEAR(p.DateHourDataCreation) = YEAR(GETDATE()) " + 
    "GROUP BY MONTH(p.DateHourDataCreation) " + 
    "ORDER BY MONTH(p.DateHourDataCreation)"); 

public class ReceitaGeral 
{ 
    [Required] 
    public virtual int MonthNumber { get; set; } 

    [Required] 
    public virtual double ValueMonth { get; set; } 
} 

public IEnumerable<T> GetReceitas2<T>(string sql) 
{ 
    var context = new NHibernateHelper(); 
    var receitas = context.ConsultaHql<T>(sql); 
    return receitas; 
} 

public IList<T> ConsultaHql<T>(string sqlQuery) 
{ 
    ISession session = GetSession(); 
    IQuery query = session.CreateQuery(sqlQuery); 
    return query.List<T>(); 
} 

Receita類

public class Receita 
    { 
    [Required] 
    public virtual int Receita_Id { get; set; } 

    [Required] 
    public virtual DateTime DateHourDataCreation{ get; set; } 

    [Required] 
    public virtual Company CodeCompany { get; set; } 

    [Required] 
    public virtual Source CodeSource { get; set; } 

    [Required] 
    public virtual Service CodeService { get; set; } 

    [Required] 
    public virtual Equipment CodeEquipment { get; set; } 

    [Required] 
    public virtual Person CodeClientPerson { get; set; } 

    [Required] 
    public virtual Person CodeSellerPerson { get; set; } 

    [Required] 
    public virtual DateTime DateEmission { get; set; } 

    [Required] 
    public virtual int Quantity { get; set; } 

    [Required] 
    public virtual double Value { get; set; } 
} 

我跑由NHibernate的生成的SQL語句,它工作在SQL Server的罰款。

UPDATE

下圖顯示的圖像,該查詢返回2倍的值,我覺得應該回到我的課ReceitaGeral參考。我該怎麼做 ?

如果我把一個HQL一句話FROM Receita它返回Receita類的引用,現在我想referece到ReceitaGeral

enter image description here

+0

是'ReceitaGeral'映射類或只是一個你用來返回查詢結果的類嗎? – 2014-10-29 13:44:49

+0

只是一個我用來返回查詢結果的類。映射的是Receita。 – 2014-10-29 14:30:37

回答

1

你幾乎沒有,只需要結果變壓器和適當的ALIAS爲每列。

別名(見AS MonthNumberAS ValueMonth

var valoresAgregadosAnoAtual2 = _repositorio.GetReceitas2<ReceitaGeral>(
    "SELECT MONTH(p.DateHourDataCreation) AS MonthNumber, SUM(p.Value) AS ValueMonth" + 
    "FROM Receita p " + 
    "WHERE YEAR(p.DateHourDataCreation) = YEAR(GETDATE()) " + 
    "GROUP BY MONTH(p.DateHourDataCreation) " + 
    "ORDER BY MONTH(p.DateHourDataCreation)"); 

變壓器(使用別名將創建的ReceitaGeral實例)

public IList<T> ConsultaHql<T>(string sqlQuery) 
{ 
    ISession session = GetSession(); 
    IQuery query = session.CreateQuery(sqlQuery); 
    return query 
     .SetResultTransformer(Transformers.AliasToBean<T>()) // e.g. T is ReceitaGeral 
     .List<T>(); 
}