在使用Hibernate处理复杂的查询语句时,譬如:
select tags.text from Tweet tweet inner join tweet.tags tags on tweet.id=tags.tweetId and tweet.keyword=:keyword and tags.text=:text;
当要加入on条件查询时:
select tags.text from Tweet tweet inner join tweet.tags tags on tweet.id=tags.tweetId and tweet.keyword=:keyword and tags.text=:text;
Hibernate会报unexpected string:on
解决办法,使用原生的SQL:
1 String sql = "select tags.text as text from tweet as tweet inner join tweet_tags as tags on tweet.id=tags.tweetId and tweet.keyword=:keyword and tags.text=:text"; 2 SQLQuery query = session.createSQLQuery(sql); 3 query.addScalar("text",Hibernate.STRING); 4 query.addScalar("ct",Hibernate.LONG); 5 query.list();