hibernate中的Query查询和criteria查询的区别

Query查询:

 
 
	<span style="font-size:18px;">session = HibernateUitl.getSession();
			String hql = "from User as user where user.name =:n";
			//得到一个Query对象
			Query query = session.createQuery(hql);
			//为hql中的属性赋值
			query.setString("n", name);
			//
			List<User>list = query.list();
			//输出查询结果
			for(User user : list){
				System.out.println(user.getname());
			}</span>

criteria查询:

<span style="font-size:18px;">session = HibernateUitl.getSession();
			//查询语句
			Criteria c = session.createCriteria(User.class);
			//添加约束条件
			c.add(Restrictions.eq("name", name));
			//得到查询结果user
			User user = (User)c.uniqueResult();</span>



你可能感兴趣的:(Hibernate,框架,HQL)