Hibernate 之SQL查询—实体查询,并将查询结果集转化成非持久化实体(即普通javaBean)

方法:利用Query接口的setResultTransformer()方法

条件:只要该普通javaBean提供settergetter方法即可

 

Eg

		Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		String queryString = "select a.user, b.dept from user a, dept b where a.deptid=b.deptid";
		List list = session.createSQLQuery(queryString).setResultTransformer(Transformers.aliasToBean(PojoBean.class)).list();
		transaction.commit();
		session.close();
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			PojoBean bean = (PojoBean) iterator.next();
			
		}

 

 

 

 

你可能感兴趣的:(sql,Hibernate,bean,xml)