查询实体的部分属性并组成实体对象(ScrollableResults的用法)

/*查询实体的部分属性并组成实体对象*/
	public void query_for_part_object(){
		Session session = null;
		ScrollableResults scrollableResults = null;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery("SELECT u.id,u.password,u.email FROM User u");
			List<User> list = new ArrayList<User>();
			scrollableResults = query.scroll();
			User user = null;
			while(scrollableResults.next()){
				user = new User();
				user.setId(scrollableResults.getInteger(0));
				user.setPassword(scrollableResults.getString(1));
				user.setEmail(scrollableResults.getString(2));
				list.add(user);
			}
			for(User u : list){
				System.out.println(u.getId());
				System.out.println(u.getPassword());
				System.out.println(u.getEmail());
			}
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			scrollableResults.close();
			HibernateSessionFactory.closeSession(session);
		}
	}



ScrollableResults的一个小例子
hibernate各种条件查询汇总
hibernate处理批量更新和批量删除
Hibernate高效查询,只查询部分/指定字段

你可能感兴趣的:(Hibernate,实体,部分属性)