nickokss
nickokss

Reputation: 25

Using sum in hibernate criteria

How can I express this query in hibernate criteria??

select SUM (tabla2.valor1) 
  from tabla1 INNER JOIN 
       tabla2 ON tabla1.ID = tabla2.ID 
 where tabla1.ID = 1

Thanks!

Upvotes: 1

Views: 2369

Answers (1)

Leviand
Leviand

Reputation: 2805

You can use Projections , something like

getSessionFactory() //this is the session factory, initialized with->  new Configuration().configure().buildSessionFactory()
    .openSession()
    .createCriteria(tabla1.class)
    .createAlias("tabla2.id", "tab2", JoinType.INNER_JOIN)
    .setProjection(Projections.sum("tab2.valor1"));

Upvotes: 2

Related Questions