Join tables hibernate + group by

梦想的初衷 提交于 2019-12-12 10:00:00

问题


I need to do this query with Java + Hibernate.

SELECT 
    table2.id,
    COUNT(table2.id) AS count
FROM
  table1 
    JOIN table2
       ON table1.fk_tb2 = table2.id  --many2one
GROUP BY
   table2.id

I would use DetachedCriteria class.....how can i do this ?


回答1:


Try using projections like this:

Criteria table1Crit = session.createCriteria("table1");
Criteria table2Crit = table1Crit.createCriteria("table2", Criteria.INNER_JOIN);
table2Crit.setProjection( Property.forName("id").count() );



回答2:


use your query with createNativeQuery method



来源:https://stackoverflow.com/questions/11146105/join-tables-hibernate-group-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!