给定sql查询的等效hibernate查询

时间:2014-08-09 17:36:18

标签: sql hibernate hql

Hibernate新手提醒。对于后面提到的sql查询,等效的hibernate查询是什么?

SELECT DISTINCT meetupid 
FROM   meetups 
   INNER JOIN friends 
           ON meetups.participantphonenumber = friends.friendphonenumber 
              AND friends.userphonenumber = :phoneNumber

我尝试过:

SELECT DISTINCT meetUp.meetUpId 
FROM MeetUp meetUp 
   INNER JOIN Friendship friends 
           ON meetUp.participantPhoneNumber = friends.friendPhoneNumber 
              AND friends.userPhoneNumber = :phoneNumber

但这会提供 ERROR: Path expected for join!

注意:表格 meetups friends 与任何外键都无关。

TIA

1 个答案:

答案 0 :(得分:0)

这不是使用HQL的好例子,因为@AVolpe在他的评论中说。但您应该能够使用子查询在HQL中执行此操作。但实际上......映射或使用SQL。

select m.meetupid from Meetup m  
    where m.participantPhoneNumber in (
        select f.friendPhoneNumber from Friendship f
            where f.userPhone = :phoneNumber)