我正在尝试将以下本机SQL查询转换为Hibernate java代码。看了SO中的所有问题,但没有帮助
SELECT TableB.username,COUNT(TableA.userId) AS NumberOfOrders
FROM TableA
LEFT JOIN TableB
ON TableA.userId=TableB.Id
GROUP BY userId;
我试过的是
criteria=hibernate.createCriteria(TableA.class);
criteria.setFetchMode("TableB",FetchMode.JOIN);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("userId"));
projectionList.add(Projections.rowCount());
criteria.setProjection(projectionList);
但它没有用。如何解决这个问题?如何将该SQL转换为Java代码?请给我一些建议