java - 如何使用spring data jpa進(jìn)行兩個(gè)列和的結(jié)果排序
問(wèn)題描述
大家好,目前有個(gè)需求就是求表中兩列的和,然后將結(jié)果排序。詳細(xì)描述如下:我的表里有兩列是費(fèi)用a(pay_a)和費(fèi)用b(pay_b),目前的需求是要將這兩者相加求和(select (pay_a + pay_b)from paytable),然后結(jié)果排序(數(shù)據(jù)庫(kù)使用的是oracle)。
我使用的框架是spring data jpa。在使用sort時(shí),發(fā)現(xiàn)sort無(wú)法解析相加的表達(dá)式。提示表中無(wú)此成員。換了個(gè)方法,在entity中用@Transient聲明一個(gè)新成員payall(數(shù)據(jù)庫(kù)表中無(wú)此列),定義getPayall(){
return pay_a + pay_b;
}然后在sort中,使用”payall“進(jìn)行排序,結(jié)果運(yùn)行錯(cuò)誤,會(huì)提示 Unable to locate Attribute with the the given name [payall] on this ManagedType [io.koala.tobacco.dataservice.entity.PotentialCust]
目前有點(diǎn)不知道要如何處理了。還請(qǐng)各位高手多幫忙!感謝感謝!
問(wèn)題解答
回答1:@Query(value = 'select new test.Account(a.price1 + a.price2) from Account a', nativeQuery = true)Account testSum();
package test;public class Account { public Account(price1, price2) { }}
nativeQuery = true 使用原生的sql進(jìn)行查詢
相關(guān)文章:
1. docker gitlab 如何git clone?2. docker鏡像push報(bào)錯(cuò)3. 關(guān)于docker下的nginx壓力測(cè)試4. 在windows下安裝docker Toolbox 啟動(dòng)Docker Quickstart Terminal 失敗!5. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問(wèn)題。6. javascript - 怎么看網(wǎng)站用了什么技術(shù)框架?7. PC 手機(jī)兼容的 編輯器8. android - 項(xiàng)目時(shí)間長(zhǎng)了,字符串文件strings有的字符串可能是多余的,有沒(méi)有辦法快速檢測(cè)那些是沒(méi)用的?9. angular.js - angularJs ngRoute怎么在路由傳遞空字符串及用ng-switch取得10. mysql - 用PHPEXCEL將excel文件導(dǎo)入數(shù)據(jù)庫(kù)數(shù)據(jù)5000+條,本地?cái)?shù)據(jù)庫(kù)正常,線上只導(dǎo)入15條,沒(méi)有報(bào)錯(cuò),哪里的問(wèn)題?
