我在 SQL Server 2005 中有以下查询:
SELECT distinct(user.id_user), user.name, user.familyname from tbl_users users
inner join tbl_notes notes on users.id_user = notes.id_user
where notes.id_book = 1
我已经映射了所有必要的类,我需要使用 hibernate API 来编写它。到目前为止,我已经编写了以下代码,但它返回 null:
hibernateSession = HibernateUtil.getSessionFactory().openSession();
criteria = hibernateSession.createCriteria(TblUsers.class, "users")
.createCriteria("users.id_user", "tblNotes")
.add(Restrictions.eq("tblNotes.idBook", idBook));
criteria.setProjection(Projections.projectionList()
.add(Projections.distinct(Projections.property("users.idUser")))
.add(Projections.property("users.name"))
.add(Projections.property("users.familyName")));
ret = criteria.list();