2016-09-16 336 views
1

休眠預測我有一個包含從該表下列字段爲彈簧引導應用

|id|feedback_status|financial_year|attendence|behaviour|total_feedback|user_id|total_behaviour| 

蔭取feedback_status和total_feedback的表,並返回作爲使用JSON數據有突起和projectionlist冬眠。

List<StudentFeedback> results = null; 

       Session session = getSession(); 
       Criteria criteria = session.createCriteria(StudentFeedback.class); 

       ProjectionList projlist = Projections.projectionList(); 
       projlist.add(Projections.property("totalFeedback").as("qty")); 
       projlist.add(Projections.groupProperty("feedbackStatus").as("feedName")); 

       criteria.setProjection(projlist); 
       criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); 
       results = criteria.list(); 
} 
    return GsonUtils.toGson(results); 
    } 

我feedback_status和total_feedback包含下列值

|feedback_status|   |total_feedback | 
    Excellent     23 
    Average      11 
    Poor      12 
    Good      13 
    Poor      12 

所以,問題是無論何時返回JSON,它打印feedback_statuspoor兩次,我想一次印刷只值並且如果它發生多於一次,則總結對應於feedback_statustotal_feedback並返回到單個。

+1

你好兄弟得到了答案, 你可以嘗試使用.setProjection(Projections.countDistinct( 「feedback_status」) ); 例如:標準條件= createCriteria(values).setProjection(Projections.countDistinct(「feedback_status」)); – Pradeep

+0

@PradeepCG對於打印出來的結果很有幫助,因爲total_feedback? – BeginnerBro

+0

嘿,你需要使用總和與計數限制,請檢查這些鏈接 http://stackoverflow.com/questions/23044565/count-with-other-fields-in-group-by-with-hibernate http: //stackoverflow.com/questions/23044565/count-with-other-fields-in-group-by-with-hibernate – Pradeep

回答

1

我用groupProperty休眠porjections

順便說一句感謝普拉迪普CG

List<StudentFeedback> results = null; 

Session session = getSession(); 
Criteria criteria = session.createCriteria(StudentFeedback.class); 

ProjectionList projlist = Projections.projectionList(); 
projlist.add(Projections.sum("totalFeedback").as("qty")); 
projlist.add(Projections.groupProperty("feedbackStatus").as("feedName")); 


criteria.setProjection(projlist); 
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); 
results = criteria.list(); 

return GsonUtils.toGson(results);