现有实体:Account和Role,Account到Role是多对多的单向关联。 需求:查找拥有某个特定Role的Account,即类似于:from Account a where a.roles contains ? 这样的查询 解决方案:HQL不提供contains的查询,但可以利用join,解决方法是:select a from Account a join a.roles r where r.id = ? 心得:HQL 的join后面不一定非要跟一个实体,不能被SQL的思维定势套牢了! 转摘者注:也可以用这种语句: select a from Account a where a.roles.id = :id 而如果是要找一个集合,则可以这样: select a from Account a where a.roles in (:roles) 后面这个可以参考:http:///questions/4638543/selecting-where-an-entity-contains-a-list-thats-a-subset-of-another-list |
|