- public static List findHBDH() {
- Transaction tx = null;
- Session session = SessionManager.currentSession();
- List<Long> list = new ArrayList<Long>();
- try {
- tx = session.beginTransaction();
- StringBuffer hqlStr = new StringBuffer(
- "select distinct xx.b1 from XXX xx");
- Query qry = session.createQuery(hqlStr.toString());
- Iterator it = qry.list().iterator();
- while (it.hasNext()) {
- list.add((Long)it.next());
- }
- } catch (Exception e) {
- if (tx != null) {
- try {
- tx.rollback();
- } catch (HibernateException ex) {
- ex.printStackTrace();
- }
- }
- e.printStackTrace();
- return null;
- } finally {
- try {
- SessionManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return list;
- }
- public static void main(String[] args) {
- //测试
- List list = new ArrayList();
- try{
- list = findHBDH();
- for(int i=0;i<=list.size()-1;i++){
- System.out.println(list.get(i));
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
注意两点: HQL的写法"select distinct 别名.字段 from 映射类名 别名"。。。
如何存储到list。。。 list.add((Long)it.next())。。。