六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

用HQL进行实体查询

[复制链接]

升级  31.2%

210

主题

210

主题

210

主题

进士

Rank: 4

积分
656
 楼主| 发表于 2013-2-3 14:36:08 | 显示全部楼层 |阅读模式
实体查询
例子1:
String hql=”from User user ”;   List list=session.CreateQuery(hql).list();

因为HQL语句与标准SQL语句相似,所以我们也可以在HQL语句中使用where字句,并且可以在where字句中使用各种表达式,比较操作符以及使用“and”,”or”连接不同的查询条件的组合。看下面的一些简单的例子:
from User user where user.age=20;

例子2(返回一个属性):
String hql= "select c.customerNamefrom Customer c“;Query query= session.createQuery(hql);Iteratorit = query.list().iterator();System.out.println(query.list().size());while(it.hasNext()) {String c = (String)it.next();System.out.println(c);}

例子3(返回多个属性):
如果返回多个属性,那么它们将被装入数组或者集合中
String hql= "select c.customerId, c.customerName“+“from Customer c“;Query query= session.createQuery(hql);Iteratorit = query.list().iterator();while(it.hasNext()) {Object[] obj= (Object[])it.next();   // List list= (List)it.next();Long id = (Long)obj[0];          //Long id = (Long)list.get(0);String name = (String)obj[1];     //String name = (String)list.get(1);System.out.println(id.longValue() + " " + name);}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表