select id,name,amount,sort
from
(select id,name,amount,sort,gift_id,count(gift_id)
from
(select * from t_lpw_gift g join t_lpw_dianxin_gift d on g.id=d.gift_id)
group by gift_id,id,name,amount,sort order by count(gift_id) desc)
where rownum <=5;
如何使用JPA createNativeQuery選擇,數據庫是OracleJPA選擇不能工作
表:
SQL> desc t_lpw_gift;
Name Type Nullable Default Comments
------ ------------ -------- ------- --------
ID INTEGER
NAME VARCHAR2(32)
PRICE NUMBER
AMOUNT INTEGER
SORT VARCHAR2(32) Y
SQL> desc t_lpw_dianxin_gift;
Name Type Nullable Default Comments
----------- ------- -------- ------- --------
GIFT_ID INTEGER
DIANXIN_ID INTEGER
GIFT_NUMBER INTEGER Y
迴歸實體是t_lpw_gift
public List<Gift> getHotGifts(int topHotGiftsNum) {
String sql = "select id,name,amount,sort from (select id,name,amount,sort,gift_id,count(gift_id) from (select * from t_lpw_gift g join t_lpw_dianxin_gift d on g.id=d.gift_id) group by gift_id,id,name,amount,sort order by count(gift_id) desc) where rownum <= :topHotGiftsNum";
return (List<Gift>)em.createNativeQuery(sql,Gift.class).setParameter("topHotGiftsNum", topHotGiftsNum).getResultList();
}
18:55: 11,473 WARN [org.hibernate.util.JDBCExceptionReporter] SQL錯誤:17006,SQLState:99999 18:55:12,992 ERROR [org.hibernate.util.JDBCExceptionReporter]列名無效()
當您直接對數據庫執行SQL(不通過JPA)時,它是否工作? –
是的,它可以工作 – Arisu
你在stacktrace中獲得了無效的列名異常嗎? – mprabhat