Use the new method in HQL to restrict the queried Fields

Source: Internet
Author: User

The method of "from XXX where XXX" is used, of course, to query the entire record and encapsulate it into the corresponding bean.

 

However, in many cases, you do not need to retrieve all the field values, especially when there are BLOB and CLOB fields. encapsulation of unwanted content will cause a huge waste of memory.

 

In this case, you can use the new HQL operation.

For example:

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?

 

Note: In the parameter list of the new object, it is best to use the alias point method instead of directly using the field name, which may report an error of an unknown field.

The premise for using this operation is that you have defined the same construction method as the preceding parameter type in the PerformanceValue class, otherwise hibernate does not recognize it.

 

OK. In this way, you can decide which fields need to be checked by hibernate in bean.

 

For another example, in the IR_Device_Pstn class, ing is used to set the class to PublicInfos.

Therefore, you must write the query method of the specified field as follows:

Define a required field construction method for IR_Device_Pstn

/** Constructor for the name query required for data extraction */<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 Duration, long userPortCapacity, <br/> long duration, 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 (); // a new object is required. <br/> this. publicInfos. setCreateDate (createDate); <br/> this. publicInfos. setModifyDate (modifyDate); <br/> this. publicInfos. setIdInProvince (idInProvince); <br/>}

Pay attention to the processing of fields and objects in the PublicInfos class.

HQL should be written as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.