在休眠条件中使用sum

时间:2018-07-27 08:29:50

标签: java sql hibernate criteria hibernate-criteria

如何用休眠条件表达此查询?

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

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用Projections之类的

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"));