日期:2014-05-19  浏览次数:20748 次

hibernate query查询语句咋写呢?
SQL code

select * from t_user_attention t1 ,(select attention_user_id from t_user_attention where fans_id='250') t2 where t1.attention_user_id='226' and  t1.fans_id=t2.attention_user_id



Java code

Query query = getSession().createQuery("咋写");
query.setParameter(0, 250);
query.setParameter(1, 226);


t_user_attention 对应实体类为 Attention

------解决方案--------------------
Query query = getSession().createSQLQuery("咋写");
query.setParameter(0, 250);
query.setParameter(1, 226);

// 这种情况貌似应该用 createSQLQuery 这个方法。
------解决方案--------------------
string sql="select * from t_user_attention t1 ,(select attention_user_id from t_user_attention where fans_id='250') t2 where t1.attention_user_id='226' and t1.fans_id=t2.attention_user_id";
先定义一个变量撒!



Query query = getSession().createQuery("sql");
query.setParameter(0, 250);
query.setParameter(1, 226);



------解决方案--------------------
Java code
String hql="select * from t_user_attention t1 ,(select attention_user_id from t_user_attention where fans_id=:fansId) t2 where t1.attention_user_id=:userId and  t1.fans_id=t2.attention_user_id";
Query query = getSession().createQuery(hql);
query.setParameter("fansId", 250);
query.setParameter("userId ", 226);

------解决方案--------------------
Java code

Query query = this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(client.class));
query.setFirstResult((pageNum - 1) * pageSize);
query.setMaxResults(pageSize);
List retList = query.list();