2017-07-05 88 views
-1
SELECT T1.ORGANIZATIONID, T1.CODE, T1.RFQCATEGORYID, T1.OWNERID, 
T1.PARTCOUNT, T1.DESCRIPTION,T1.STATUSCODE, T1."COMMENT", T1.CREATEID, 
T1.CREATEDATE, T1.UPDATEID, T1.UPDATEDATE, T2.CODE, T2.DESCRIPTION, 
T3.ORGANIZATIONNAME, T3.ORGANIZATIONURL, T4.SALUTATION, T4.FIRSTNAME, 
T4.LASTNAME, T4.EMAIL,T4.FINANCIAL_LIMIT, T5.ORGANIZATIONNAME 
USR_ORGNAME,T5.ORGANIZATIONURL USR_ORGURL, T6.STARTDATE, T6.ENDDATE, 
T6.OPENDATE, T6.RFQID, T6.PARTNUMBER, 
T7.RFQTYPEID,T1.CURRENCYID,T1.MINQUOTECOUNT,T1.ISMATCHING,T1.ISMULTICURR, 
T1.ESTIMATEDVALUE,T1.RAENABLED,T1.PAYMENTENABLED,T1.ISPRICEACPTINRA, 
T8.DESCRIPTION AS RFQTYPEDESC , T1.FOREIGNCURRONLY , T1.DELEGATE , 
T1.LOCATIONID ,T6.INITIATERAID ,T10.AUTHENTICATIONCODE 
,T6.DOCUMENTSALEENDDATE,T1.OWNER_DIGITAL_CERT_ID , T1.ANNUALVALUE , 
CASE WHEN (SELECT COUNT(1) FROM EPS.ORGLOCATION WHERE LOCATION_ID = 
T1.LOCATIONID AND ORGID = T1.ORGANIZATIONID) >0 THEN (SELECT 
DESCRIPTION FROM EPS.ORGLOCATION WHERE LOCATION_ID = T1.LOCATIONID AND 
ORGID = T1.ORGANIZATIONID) ELSE '' END AS LOCATIONDESC, 
T1.MATIHINGVALUE,T1.SKIPQUOTE,T1.WCENABLED,T1.LOADTYPE,T6.DISALLOWBIDREV,T6.UPDATEDATE 
AS RFQDATE_UPDATEDATE,T1.SIGNED,T1.ENCRYPTED,T1.OLDCODE 
,T1.IMPACTBIDFLAG,T1.LOADING_EVALUATED,T1.ENCKEYENABLED,T1.VALUEFLAG,od.code 
as designation, (SELECT MIN(UPDATEDATE) FROM EPS.HRFQ WHERE RFQID = 
T1.RFQ_ID AND STATUSCODE= 'A') PUBLISH_DATE,LCNCMATCHING, T1.BOQ, 
B.DESCRIPTION AS BOQDESC, T6.PHYSICALDOCSUBENDDATE, T1.AUCTIONSTYLE, 
T1.AUCTIONTYPE, T1.AUCTIONSTATUS, T1.AUCTIONFILENAME, 
T1.AUCTIONCOMMENT,T1.ITEMSUBMITBY, T1.AUCTIONWINNER FROM EPS.RFQ T1 
LEFT JOIN EPS.BOQ B ON T1.BOQ=B.BOQ_ID left outer join (select * from 
EPS.RFQDATE where partnumber=?) T6 on t6.RFQID=t1.RFQ_ID left outer 
join EPS.RFQRFQTYPE T7 on t7.RFQID=t1.RFQ_ID,EPS.RFQCAT T2, 
EPS.ORGANIZATION T3,EPS."USER" T4 left outer join eps.orgdesignation 
od on od.designation_id=t4.designationid, EPS.ORGANIZATION T5, 
EPS.AUTHENTICATION T10, EPS.RFQTYPE T8 WHERE T1.RFQCATEGORYID = 
T2.RFQCAT_ID AND T1.ORGANIZATIONID = T3.ORGANIZATION_ID AND T1.OWNERID 
= T4.USER_ID AND T4.ORGANIZATIONID = T5.ORGANIZATION_ID AND T10.USERID = T4.USER_ID AND T4.ORGANIZATIONID = T10.ORGANIZATIONID AND T1.RFQ_ID = ? AND T8.RFQTYPE_ID = T7.RFQTYPEID 

我想將它轉換成hibernate。我怎樣才能做到這一點?如何使用Hibernate將此原生SQL轉換爲HQL/JPQL或Criteria查詢?

回答

0

你有兩種選擇。

  1. 如果要按原樣使用SQL,請使用createNativeQuery以提供潛在的數據庫相關SQL。您可以提供可以跨所有潛在數據庫供應商運行的兼容SQL,以避免任何數據庫依賴性問題,但這絕對是一種選擇,特別是在需要使用某些數據庫功能時。
  2. 使用JPQL/HQL或Criteria API。這本質上是一個SQL本身的抽象,它允許你用一種語言編寫你的查詢或者對數據庫不知道的方式編寫你的查詢,因爲Hibernate會將它翻譯成適合你的數據庫平臺的語法。

你的本機查詢是有點複雜,所以我的建議,如果你想使用選項(2)。將它分解成簡單的部分,並建立在每一步反覆,直到你有你所需要的。

首先要注意的是你的基本查詢是針對由ID名爲RFQ表。如果您沒有代表RFQ表的實體,請創建一個實體並公開您嘗試從此基表返回的基本字符串和數字屬性。然後,以查詢針對這一點,你會寫

List<Rfq> rfqs = session 
    .createQuery("SELECT r FROM Rfq r WHERE r.id = :id", Rfq.class) 
    .setParameter("id", rfqId) 
    .getResultList(); 

從這一點來說它的建造更多的實體和關係的RFQ表和連接表,每一個屬性之間,然後返回它們作爲的問題根據需要選擇子句的一部分。