2

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 ?

enfix
  • 6,680
  • 12
  • 55
  • 80

2 Answers2

2

Try using projections like this:

Criteria table1Crit = session.createCriteria("table1");
Criteria table2Crit = table1Crit.createCriteria("table2", Criteria.INNER_JOIN);
table2Crit.setProjection( Property.forName("id").count() );
Matt
  • 11,523
  • 2
  • 23
  • 33
-3

use your query with createNativeQuery method

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
storm_buster
  • 7,362
  • 18
  • 53
  • 75