UserRolesDAO { public Map<Integer,String> diffRoleList() { Object ob[]; Session session = sessionFactory.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(UserRoles.class); ProjectionList p1=Projections.projectionList(); p1.add(Projections.property("role")); p1.add(Projections.property("userRoleId")); criteria.setProjection(p1); List l=criteria.list(); Iterator it=l.iterator(); Map<Integer,String> diffRoles=new HashMap<>(); while(it.hasNext()) { ob = (Object[])it.next(); diffRoles.put( (Integer) ob[1],(String) ob[0]); } session.getTransaction().commit(); session.close(); return diffRoles; }  List<UserRoles> list(Integer pageNum, Integer numResults); Map<Integer,String> diffRoleList(); List<UserRoles> listByName(String userName); UserRoles minUserRoleId(String userName); Integer totalRecordCount(); UserRoles get(Integer id); Integer insert(UserRoles userRoles); void update(UserRoles userRoles); void delete(Integer id);  }
@Test public void testDiffRoles() throws Exception{ Map<Integer,String> objects=userRolesDAO.diffRoleList(); Iterator it = objects.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); it.remove(); } }