0

次のようなHQLクエリの実行に問題があります。

select new myPackage.view.CoverDocumentReportView(Re.code AS fulCd,
Re.creditPrice AS crtprc,
Re.debitPrice  AS dbtprc,
(Re.debitPrice - Re.debitPrice) AS redbtprc,
(Re.creditPrice- Re.creditPrice) AS recrtprc,
(Re.debitPrice-Re.creditPrice)   AS rem) 
from 
(select  fullCode as code, 
     sum(creditPrice) as creditPrice ,
     sum(debitPrice)  as debitPrice   
from    DocumentMaster DM,
     DocumentAccount   DA,
     Tree              T ,
     AccountTree       AT, 
     DocumentDetailed  DD 
where DM.id =  DA.documentMaster  and  
      DA.accountTree =  T.id      and   
      DA.accountTree =  AT.id     and   
      DD.documentAccount =  DA.id 
group by  DA.accountTree ) As Re


1) これを次のように実行すると:

SQLQuery crit = (SQLQuery) session
            .createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(CoverDocumentReportView.class));
ArrayList<CoverDocumentReportView> li =  (ArrayList<CoverDocumentReportView>) crit.list();

エラー2012-12-2214:16:19,838 [http-8080-1] org.hibernate.util.JDBCExceptionReporter:SQL構文にエラーがあります。1行目の'.datx.web.accounting.view.CoverDocumentReportView(Re.code AS fulCd、Re.creditP'の近くで使用する正しい構文については、MySQLサーバーのバージョンに対応するマニュアルを確認してください。


2) これで実行した場合:

Query query = session.createQuery(sql).setResultTransformer(Transformers.aliasToBean(CoverDocumentReportView.class));
ArrayList<CoverDocumentReportView> li =  (ArrayList<CoverDocumentReportView>)query.list();

エラーは次のようになります。

エラー2012-12-2214:51:46,709 [http-8080-1] org.hibernate.hql.ast.ErrorCounter:行1:224:予期しないトークン:(エラー2012-12-22 14:51:46,709 [http -8080-1] org.hibernate.hql.ast.ErrorCounter:1行目:予期しないトークン:合計

何が問題ですか?

4

1 に答える 1

0

SQLとHQLは2つの異なる言語です。

HQLはfrom句のサブクエリをサポートしていないため、このクエリをHQLクエリにすることはできません。

また、SQLはJavaオブジェクトを認識しておらず、Javaオブジェクトnew()を作成できる関数も持っていないため、このクエリも有効なSQLクエリではありません。

これを有効なSQLクエリにして、を使用して実行しcreateSQLQuery()、結果を反復処理して、返された行からオブジェクトのインスタンスを作成します。または、結果トランスフォーマーを使用してください。これにより、結果トランスフォーマーが自動的に実行されます。結果トランスフォーマーは、SQLクエリの返された列に割り当てたエイリアスを使用してBeanを作成します。new CoverDocumentReportView()それを機能させるためにクエリに何も必要ありません。詳細については、javadocをお読みください。

于 2012-12-22T11:40:59.910 に答える