如何迭代以下代码以获取输出
String hql=" select UserName,userId FROM org.vishwa.hibernate.UserDetails where userId>=?";
Query query = session.createQuery(hql);
query.setParameter(0, 5);
List users = query.list();
选择完整的对象我能够迭代 。选择部分对象无法迭代请提供解决方案
答案 0 :(得分:1)
请使用命名参数(:userId
)代替参考参数。这种查询会返回List<Object[]>
String hql = "select userName, userId from UserDetails where userId >= :userId";
Query query = session.createQuery(hql);
query.setParameter("userId", 5);
List<Object[]> users = query.list();
for(Object[] user : users) {
//user[0] userName
//user[1] userId
}