在HQL中使用new的方式限制查取的欄位

來源:互聯網
上載者:User

使用"from XXX where XXX"的方式,當然是將整條記錄查取出來封裝進相應的bean裡

 

但是很多時候,你並不需要查取出全部的欄位值,特別是有BLOB,CLOB欄位的時候,把不需要的內容查取封裝進來,會造成巨大的記憶體浪費.

 

這時候就可以使用HQL的new操作

類似如下:

 select new PerformanceValue(P.id,P.regionNo,P.regionName,P.reportDate,P.perfCode,P.perfName,P.unit,P.value) from PerformanceValue P where P.regionNo=? and P.reportDate=? and P.perfCode in ?

 

注意在new的對象的參數列表裡,最好使用別名點取的方式,而不是直接使用欄位名稱,可能報未知欄位的錯誤

使用這種操作的前提是,你在類 PerformanceValue裡已經定義了與上述參數類型相同的構造方法,否則hibernate是不認的

 

OK,使用這樣的方式,你就可以自由的決定本操作需要hibernate給你查取哪些欄位封裝進bean裡了

 

再舉一例,在類IR_Device_Pstn中,使用組建映射,映射組建類為PublicInfos

那麼要寫成上面的限定欄位查詢方式,應該這樣寫:

定義IR_Device_Pstn的一個需要的欄位的構造方法

/**為資料幫浦需要的命名查詢準備的構造方法*/<br />public IR_Device_Pstn(<br />String provinceName, String provinceCode, String cityName, String cityCode,<br />String siteName, String deviceName, String vendorName, String deviceModel,<br />String softwareVersionBig, String softwareVersionSma, byte roles,<br />String otherRoles, long e1Capacity, long e1CapacityOccupied, long userPortCapacity,<br />long userPortCapacityOccupied, long freeRelayBoardNum, long freeUserBoardNum,<br />Date startTime, byte assetUseStatus, String parentAssetCardNo, String assetCardNo,<br />Date createDate,Date modifyDate,long idInProvince){<br />this.provinceName = provinceName;<br />this.provinceCode = provinceCode;<br />this.cityName = cityName;<br />this.cityCode = cityCode;<br />this.siteName = siteName;<br />this.deviceName = deviceName;<br />this.vendorName = vendorName;<br />this.deviceModel = deviceModel;<br />this.softwareVersionBig = softwareVersionBig;<br />this.softwareVersionSma = softwareVersionSma;<br />this.roles = roles;<br />this.otherRoles = otherRoles;<br />this.e1Capacity = e1Capacity;<br />this.e1CapacityOccupied = e1CapacityOccupied;<br />this.userPortCapacity = userPortCapacity;<br />this.userPortCapacityOccupied = userPortCapacityOccupied;<br />this.freeRelayBoardNum = freeRelayBoardNum;<br />this.freeUserBoardNum = freeUserBoardNum;<br />this.startTime = startTime;<br />this.assetUseStatus = assetUseStatus;<br />this.parentAssetCardNo = parentAssetCardNo;<br />this.assetCardNo = assetCardNo;<br />publicInfos = new PublicInfos();//必須new一個對象出來<br />this.publicInfos.setCreateDate(createDate);<br />this.publicInfos.setModifyDate(modifyDate);<br />this.publicInfos.setIdInProvince(idInProvince);<br />}

注意上面對PublicInfos類的欄位及對象的處理

HQL應該這樣寫:

select new IR_Device_Pstn(p.provinceName,p.provinceCode,p.cityName,<br />p.cityCode,p.siteName,p.deviceName,p.vendorName,p.deviceModel,p.softwareVersionBig,<br />p.softwareVersionSma,p.roles,p.otherRoles,p.e1Capacity,p.e1CapacityOccupied,p.userPortCapacity,<br />p.userPortCapacityOccupied,p.freeRelayBoardNum,p.freeUserBoardNum,p.startTime,p.assetUseStatus,<br />p.parentAssetCardNo,p.assetCardNo,p.publicInfos.createDate,p.publicInfos.modifyDate,<br />p.publicInfos.idInProvince) from IR_Device_Pstn p

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.