2016-11-28 95 views
0

我正在使用hibernate,在哪裏獲取我正在使用條件的數據。但我認爲使用標準有一些限制。但我想問下面的事情,我覺得可能通過在休眠條件使用。我們可以在hibernate的標準查詢中有標準嗎?

怎麼下面的查詢可以在標準的基礎被寫入冬眠

select * from (select d.priceId , sum(d.price) as sumP, avg(d.price) as avgP from data d group by d.priceId) as adata where sumP like '%%'; 

至於現在,我可以寫標準的基礎Hibernate查詢下面的SQL:

select d.priceId , sum(d.price) as sumP, avg(d.price) as avgP from data d group by d.priceId 

如下

Session session = sessionFactory.getCurrentSession(); 
criteria = session.createCriteria(Data.class, "data") 
criteria.setProjection(Projections.projectionList() 
.add(Projections.groupProperty("d.priceId")) 
.add(Projections.sqlProjection("sum(d.price) as sumP", new String[] {"sumP"},new org.hibernate.type.BigDecimalType[] {new org.hibernate.type.BigDecimalType()}), "sumP") 
.add(Projections.sqlProjection("avg(d.price) as avgP", new String[] {"avgP"},new org.hibernate.type.BigDecimalType[] {new org.hibernate.type.BigDecimalType()}), "avgP")); 

那麼如何在下面的SQL中編寫基於標準的查詢:

select * from (select d.priceId , sum(d.price) as sumP, avg(d.price) as avgP from data d group by d.priceId) as adata where sumP like '%%'; 

請幫我上面。

+0

您正在使用Hibernate API,而不是JPA API,因此不要標記爲JPA –

回答